Skip to content

Commit

Permalink
Add world.breakBlock, world.getBlock, and rename World.setBlock to Wo…
Browse files Browse the repository at this point in the history
…rld.placeBlock
  • Loading branch information
RevolvingMadness committed Jan 29, 2024
1 parent f6685b6 commit 156292e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@
- `ceil(object: Integer | Float): Integer`
- `floor(object: Integer | Float): Integer`
- `BlockHitResult`
- `World.breakBlock(pos: BlockPos, drop_items: Boolean): Boolean`
- `World.getBlock(pos: BlockPos): Block`

### Changed

- `events.onPlayerBreakBlock(function: Function[Boolean, PlayerEntity, Block]): Null` is
now `events.onPlayerBreakBlock(function: Function[Boolean, PlayerEntity, BlockPos, Block]): Null`
- `World.setBlock` renamed to `World.placeBlock`

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import com.revolvingmadness.sculk.language.builtins.classes.instances.BooleanInstance;
import com.revolvingmadness.sculk.language.builtins.classes.instances.NullInstance;
import com.revolvingmadness.sculk.language.builtins.classes.instances.StringInstance;
import com.revolvingmadness.sculk.language.builtins.classes.instances.WorldInstance;
import com.revolvingmadness.sculk.language.interpreter.Interpreter;
import com.revolvingmadness.sculk.language.lexer.TokenType;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

import java.util.List;

Expand All @@ -22,6 +25,24 @@ public PlayerEntityType() {
this.typeVariableScope.declare(List.of(TokenType.CONST), "isSpectator", new IsSpectator());
this.typeVariableScope.declare(List.of(TokenType.CONST), "getName", new GetName());
this.typeVariableScope.declare(List.of(TokenType.CONST), "getUUID", new GetUUID());
this.typeVariableScope.declare(List.of(TokenType.CONST), "getWorld", new GetWorld());
}

private static class GetWorld extends BuiltinMethod {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
if (arguments.size() != 0) {
throw ErrorHolder.invalidArgumentCount("getWorld", 0, arguments.size());
}

World world = this.boundClass.toPlayerEntity().getWorld();

if (!(world instanceof ServerWorld serverWorld)) {
throw new RuntimeException("World is on client");
}

return new WorldInstance(serverWorld);
}
}

private static class AddExperienceLevels extends BuiltinMethod {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public WorldType() {
this.typeVariableScope.declare(List.of(TokenType.CONST), "getSeed", new GetSeed());
this.typeVariableScope.declare(List.of(TokenType.CONST), "setSpawnPos", new SetSpawnPos());
this.typeVariableScope.declare(List.of(TokenType.CONST), "setTimeOfDay", new SetTimeOfDay());
this.typeVariableScope.declare(List.of(TokenType.CONST), "setBlock", new SetBlock());
this.typeVariableScope.declare(List.of(TokenType.CONST), "placeBlock", new PlaceBlock());
this.typeVariableScope.declare(List.of(TokenType.CONST), "breakBlock", new BreakBlock());
this.typeVariableScope.declare(List.of(TokenType.CONST), "getBlock", new GetBlock());
}

private static class CanSetBlock extends BuiltinMethod {
Expand Down Expand Up @@ -188,23 +190,23 @@ public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments)
}
}

private static class SetBlock extends BuiltinMethod {
private static class PlaceBlock extends BuiltinMethod {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
if (arguments.size() != 2) {
throw ErrorHolder.invalidArgumentCount("setBlock", 2, arguments.size());
throw ErrorHolder.invalidArgumentCount("placeBlock", 2, arguments.size());
}

BuiltinClass blockPosClass = arguments.get(0);

if (!blockPosClass.instanceOf(new BlockPosType())) {
throw ErrorHolder.argumentRequiresType(1, "setBlock", new BlockPosType(), blockPosClass.getType());
throw ErrorHolder.argumentRequiresType(1, "placeBlock", new BlockPosType(), blockPosClass.getType());
}

BuiltinClass blockClass = arguments.get(1);

if (!blockClass.instanceOf(new BlockType())) {
throw ErrorHolder.argumentRequiresType(2, "setBlock", new BlockType(), blockClass.getType());
throw ErrorHolder.argumentRequiresType(2, "placeBlock", new BlockType(), blockClass.getType());
}

BlockPos blockPos = blockPosClass.toBlockPos();
Expand All @@ -214,6 +216,51 @@ public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments)
}
}

private static class BreakBlock extends BuiltinMethod {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
if (arguments.size() != 2) {
throw ErrorHolder.invalidArgumentCount("breakBlock", 2, arguments.size());
}

BuiltinClass blockPosClass = arguments.get(0);

if (!blockPosClass.instanceOf(new BlockPosType())) {
throw ErrorHolder.argumentRequiresType(1, "breakBlock", new BlockPosType(), blockPosClass.getType());
}

BuiltinClass dropItemsClass = arguments.get(1);

if (!dropItemsClass.instanceOf(new BooleanType())) {
throw ErrorHolder.argumentRequiresType(2, "breakBlock", new BooleanType(), dropItemsClass.getType());
}

BlockPos blockPos = blockPosClass.toBlockPos();
boolean dropItems = dropItemsClass.toBoolean();

return new BooleanInstance(this.boundClass.toWorld().breakBlock(blockPos, dropItems));
}
}

private static class GetBlock extends BuiltinMethod {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
if (arguments.size() != 1) {
throw ErrorHolder.invalidArgumentCount("getBlock", 1, arguments.size());
}

BuiltinClass blockPosClass = arguments.get(0);

if (!blockPosClass.instanceOf(new BlockPosType())) {
throw ErrorHolder.argumentRequiresType(1, "getBlock", new BlockPosType(), blockPosClass.getType());
}

BlockPos blockPos = blockPosClass.toBlockPos();

return new BlockInstance(this.boundClass.toWorld().getBlockState(blockPos).getBlock());
}
}

private static class SetSpawnPos extends BuiltinMethod {
@Override
public BuiltinClass call(Interpreter interpreter, List<BuiltinClass> arguments) {
Expand Down

0 comments on commit 156292e

Please sign in to comment.