From bda4e018077b2ced6b78eabf6c22c1032639ffaa Mon Sep 17 00:00:00 2001 From: LabyStudio Date: Tue, 1 Feb 2022 11:21:34 +0100 Subject: [PATCH] implement block picking and remove water collision --- src/js/net/minecraft/client/Minecraft.js | 13 ++++++++++++- src/js/net/minecraft/client/world/World.js | 17 +++++++++++------ .../net/minecraft/client/world/block/Block.js | 4 ++++ .../minecraft/client/world/block/BlockWater.js | 5 ++++- 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/js/net/minecraft/client/Minecraft.js b/src/js/net/minecraft/client/Minecraft.js index 7fb2f03..e39e82b 100644 --- a/src/js/net/minecraft/client/Minecraft.js +++ b/src/js/net/minecraft/client/Minecraft.js @@ -20,6 +20,7 @@ window.Minecraft = class { // Create player this.player = new Player(this.world); + this.pickedBlock = 1; // Initialize this.init(); @@ -98,6 +99,16 @@ window.Minecraft = class { } } + // Pick block + if (button === 1) { + if (hitResult != null) { + let typeId = this.world.getBlockAt(hitResult.x, hitResult.y, hitResult.z); + if (typeId !== 0) { + this.pickedBlock = typeId; + } + } + } + // Place block if (button === 2) { if (hitResult != null) { @@ -109,7 +120,7 @@ window.Minecraft = class { // Don't place blocks if the player is standing there if (!placedBoundingBox.intersects(this.player.boundingBox)) { - this.world.setBlockAt(x, y, z, 1); + this.world.setBlockAt(x, y, z, this.pickedBlock); } } } diff --git a/src/js/net/minecraft/client/world/World.js b/src/js/net/minecraft/client/world/World.js index 0fcc014..35e8a6a 100644 --- a/src/js/net/minecraft/client/world/World.js +++ b/src/js/net/minecraft/client/world/World.js @@ -132,10 +132,12 @@ window.World = class { let blockId = this.getBlockAt(x, y, z); let block = Block.getById(blockId); - let hit = block == null ? null : block.collisionRayTrace(x, y, z, from, to); - if (hit != null) { - return hit; + if (block != null && block.canCollide()) { + let hit = block.collisionRayTrace(x, y, z, from, to); + if (hit != null) { + return hit; + } } let lastHit = null; @@ -224,9 +226,12 @@ window.World = class { let blockId = this.getBlockAt(x, y, z); let block = Block.getById(blockId); - let hit = block == null ? null : block.collisionRayTrace(x, y, z, from, to); - if (hit != null) { - return hit; + + if (block != null && block.canCollide()) { + let hit = block.collisionRayTrace(x, y, z, from, to); + if (hit != null) { + return hit; + } } } diff --git a/src/js/net/minecraft/client/world/block/Block.js b/src/js/net/minecraft/client/world/block/Block.js index dc00058..c9bfd93 100644 --- a/src/js/net/minecraft/client/world/block/Block.js +++ b/src/js/net/minecraft/client/world/block/Block.js @@ -47,6 +47,10 @@ window.Block = class { return 1.0; } + canCollide() { + return true; + } + getBoundingBox(world, x, y, z) { return this.boundingBox; } diff --git a/src/js/net/minecraft/client/world/block/BlockWater.js b/src/js/net/minecraft/client/world/block/BlockWater.js index 89855ab..a46be8e 100644 --- a/src/js/net/minecraft/client/world/block/BlockWater.js +++ b/src/js/net/minecraft/client/world/block/BlockWater.js @@ -12,6 +12,10 @@ window.BlockWater = class extends Block { return false; } + canCollide() { + return false; + } + shouldRenderFace(world, x, y, z, face) { let typeId = world.getBlockAtFace(x, y, z, face); return typeId === 0 || typeId !== this.id && Block.getById(typeId).isTransparent(); @@ -24,5 +28,4 @@ window.BlockWater = class extends Block { } return box; } - } \ No newline at end of file