Skip to content

Commit

Permalink
Add some more #3 and maybe fix a bug idk tho
Browse files Browse the repository at this point in the history
  • Loading branch information
RevolvingMadness committed Mar 26, 2024
1 parent 2faaa44 commit b08970a
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 924 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,6 @@ public BuiltinClassType(BuiltinClassType type, List<TokenType> accessModifiers,
}
}

public void addGetterMethod(String name, Function<BuiltinClass, BuiltinClass> supplier) {
BuiltinMethod method = new BuiltinMethod() {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
this.validateCall(name, arguments);

return supplier.apply(this.boundClass);
}
};

this.typeVariableScope.declare(List.of(TokenType.CONST), name, method);
}

public void addMethod(String name, List<BuiltinClassType> argumentTypes) throws ReflectiveOperationException {
Method method = this.getClass().getMethod(name, Interpreter.class, BuiltinClass.class, BuiltinClass[].class);

Expand Down Expand Up @@ -102,6 +89,19 @@ public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments)
this.typeVariableScope.declare(List.of(TokenType.CONST), method.getName(), methodClass);
}

public void addNoArgMethod(String name, Function<BuiltinClass, BuiltinClass> supplier) {
BuiltinMethod method = new BuiltinMethod() {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
this.validateCall(name, arguments);

return supplier.apply(this.boundClass);
}
};

this.typeVariableScope.declare(List.of(TokenType.CONST), name, method);
}

public void addStaticMethod(String name, List<BuiltinClassType> argumentTypes) throws ReflectiveOperationException {
Method method = this.getClass().getMethod(name, Interpreter.class, BuiltinClass.class, BuiltinClass[].class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ private PlayerManagerClassType() {
super("PlayerManager");

try {
this.addGetterMethod("areCheatsEnabled", builtinClass -> new BooleanInstance(builtinClass.toPlayerManager().areCheatsAllowed()));
this.addGetterMethod("getCurrentPlayerCount", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getCurrentPlayerCount()));
this.addGetterMethod("getMaxPlayerCount", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getMaxPlayerCount()));
this.addGetterMethod("getSimulationDistance", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getSimulationDistance()));
this.addGetterMethod("getViewDistance", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getViewDistance()));
this.addGetterMethod("isWhitelistEnabled", builtinClass -> new BooleanInstance(builtinClass.toPlayerManager().isWhitelistEnabled()));
this.addNoArgMethod("areCheatsEnabled", builtinClass -> new BooleanInstance(builtinClass.toPlayerManager().areCheatsAllowed()));
this.addNoArgMethod("getCurrentPlayerCount", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getCurrentPlayerCount()));
this.addNoArgMethod("getMaxPlayerCount", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getMaxPlayerCount()));
this.addNoArgMethod("getSimulationDistance", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getSimulationDistance()));
this.addNoArgMethod("getViewDistance", builtinClass -> new IntegerInstance(builtinClass.toPlayerManager().getViewDistance()));
this.addNoArgMethod("isWhitelistEnabled", builtinClass -> new BooleanInstance(builtinClass.toPlayerManager().isWhitelistEnabled()));
this.addMethod("setCheatsEnabled", List.of(BooleanClassType.TYPE));
this.addMethod("setSimulationDistance", List.of(IntegerClassType.TYPE));
this.addMethod("setViewDistance", List.of(IntegerClassType.TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,24 @@ private WorldClassType() {
this.addMethod("breakBlock", List.of(BlockPosClassType.TYPE, BooleanClassType.TYPE));
this.addMethod("canSetBlock", List.of(BlockPosClassType.TYPE));
this.addMethod("getBlock", List.of(BlockPosClassType.TYPE));
this.addGetterMethod("getPlayers", builtinClass -> {
this.addNoArgMethod("getPlayers", builtinClass -> {
List<ServerPlayerEntity> players = builtinClass.toWorld().getPlayers();
List<BuiltinClass> playersClasses = new ArrayList<>();

players.forEach(serverPlayerEntity -> playersClasses.add(new ServerPlayerEntityInstance(serverPlayerEntity)));

return new ListInstance(playersClasses);
});
this.addGetterMethod("getSeed", builtinClass -> new IntegerInstance(builtinClass.toWorld().getSeed()));
this.addGetterMethod("getTime", builtinClass -> new IntegerInstance(builtinClass.toWorld().getTime()));
this.addGetterMethod("getTimeOfDay", builtinClass -> new IntegerInstance(builtinClass.toWorld().getTimeOfDay()));
this.addNoArgMethod("getSeed", builtinClass -> new IntegerInstance(builtinClass.toWorld().getSeed()));
this.addNoArgMethod("getTime", builtinClass -> new IntegerInstance(builtinClass.toWorld().getTime()));
this.addNoArgMethod("getTimeOfDay", builtinClass -> new IntegerInstance(builtinClass.toWorld().getTimeOfDay()));
this.addMethod("hasRain", List.of(BlockPosClassType.TYPE));
this.addGetterMethod("isDay", builtinClass -> new BooleanInstance(builtinClass.toWorld().isDay()));
this.addGetterMethod("isFlat", builtinClass -> new BooleanInstance(builtinClass.toWorld().isFlat()));
this.addGetterMethod("isNight", builtinClass -> new BooleanInstance(builtinClass.toWorld().isNight()));
this.addGetterMethod("isRaining", builtinClass -> new BooleanInstance(builtinClass.toWorld().isRaining()));
this.addGetterMethod("isSleepingEnabled", builtinClass -> new BooleanInstance(builtinClass.toWorld().isSleepingEnabled()));
this.addGetterMethod("isThundering", builtinClass -> new BooleanInstance(builtinClass.toWorld().isThundering()));
this.addNoArgMethod("isDay", builtinClass -> new BooleanInstance(builtinClass.toWorld().isDay()));
this.addNoArgMethod("isFlat", builtinClass -> new BooleanInstance(builtinClass.toWorld().isFlat()));
this.addNoArgMethod("isNight", builtinClass -> new BooleanInstance(builtinClass.toWorld().isNight()));
this.addNoArgMethod("isRaining", builtinClass -> new BooleanInstance(builtinClass.toWorld().isRaining()));
this.addNoArgMethod("isSleepingEnabled", builtinClass -> new BooleanInstance(builtinClass.toWorld().isSleepingEnabled()));
this.addNoArgMethod("isThundering", builtinClass -> new BooleanInstance(builtinClass.toWorld().isThundering()));
this.addMethod("placeBlock", List.of(BlockPosClassType.TYPE, BlockClassType.TYPE));
this.addMethod("setSpawnPos", List.of(BlockPosClassType.TYPE, FloatClassType.TYPE));
this.addMethod("setTimeOfDay", List.of(IntegerClassType.TYPE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ public class BlockClassType extends NBTBuiltinClassType {
private BlockClassType() {
super("Block");

this.addGetterMethod("asItem", builtinClass -> new ItemInstance(builtinClass.toBlock().asItem()));
this.addGetterMethod("getBlastResistance", builtinClass -> new FloatInstance(builtinClass.toBlock().getBlastResistance()));
this.addGetterMethod("getName", builtinClass -> new StringInstance(builtinClass.toBlock().getName().getString()));
this.addGetterMethod("getSlipperiness", builtinClass -> new FloatInstance(builtinClass.toBlock().getSlipperiness()));
this.addNoArgMethod("asItem", builtinClass -> new ItemInstance(builtinClass.toBlock().asItem()));
this.addNoArgMethod("getBlastResistance", builtinClass -> new FloatInstance(builtinClass.toBlock().getBlastResistance()));
this.addNoArgMethod("getName", builtinClass -> new StringInstance(builtinClass.toBlock().getName().getString()));
this.addNoArgMethod("getSlipperiness", builtinClass -> new FloatInstance(builtinClass.toBlock().getSlipperiness()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private ListClassType() {
try {
this.addMethod("append", List.of(ObjectClassType.TYPE));
this.addMethod("contains", List.of(ObjectClassType.TYPE));
this.addGetterMethod("length", builtinClass -> new IntegerInstance(builtinClass.toList().size()));
this.addNoArgMethod("length", builtinClass -> new IntegerInstance(builtinClass.toList().size()));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,10 @@

@SuppressWarnings("unused")
public class StringClassType extends NBTBuiltinClassType {
public static final StringClassType TYPE = new StringClassType();
public static final StringClassType TYPE;

private StringClassType() {
super("String");

try {
this.addMethod("startsWith", List.of(StringClassType.TYPE));
this.addMethod("endsWith", List.of(StringClassType.TYPE));
this.addMethod("split", List.of(StringClassType.TYPE));
this.addGetterMethod("length", builtinClass -> new IntegerInstance(builtinClass.toString().length()));
this.addGetterMethod("lowercase", builtinClass -> new StringInstance(builtinClass.toString().toLowerCase()));
this.addGetterMethod("uppercase", builtinClass -> new StringInstance(builtinClass.toString().toUpperCase()));
this.addMethod("fromUnicode", List.of(IntegerClassType.TYPE));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}

public BuiltinClass endsWith(Interpreter interpreter, BuiltinClass boundClass, BuiltinClass[] arguments) {
Expand Down Expand Up @@ -66,4 +54,20 @@ public BuiltinClass split(Interpreter interpreter, BuiltinClass boundClass, Buil
public BuiltinClass startsWith(Interpreter interpreter, BuiltinClass boundClass, BuiltinClass[] arguments) {
return new BooleanInstance(boundClass.toString().startsWith(arguments[0].toString()));
}

static {
TYPE = new StringClassType();

try {
TYPE.addMethod("startsWith", List.of(StringClassType.TYPE));
TYPE.addMethod("endsWith", List.of(StringClassType.TYPE));
TYPE.addMethod("split", List.of(StringClassType.TYPE));
TYPE.addNoArgMethod("length", builtinClass -> new IntegerInstance(builtinClass.toString().length()));
TYPE.addNoArgMethod("lowercase", builtinClass -> new StringInstance(builtinClass.toString().toLowerCase()));
TYPE.addNoArgMethod("uppercase", builtinClass -> new StringInstance(builtinClass.toString().toUpperCase()));
TYPE.addMethod("fromUnicode", List.of(IntegerClassType.TYPE));
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
}
Loading

0 comments on commit b08970a

Please sign in to comment.