diff --git a/src/js/Start.js b/src/js/Start.js index 14a55a27..3e1d93bf 100644 --- a/src/js/Start.js +++ b/src/js/Start.js @@ -42,7 +42,8 @@ class Start { "gui/title/background/panorama_3.png", "gui/title/background/panorama_4.png", "gui/title/background/panorama_5.png", - "gui/container/creative.png" + "gui/container/creative.png", + "gui/container/crafting_table.png", ]).then((resources) => { // Launch actual game on canvas window.app = new Minecraft(canvasWrapperId, resources); diff --git a/src/js/net/minecraft/client/Minecraft.js b/src/js/net/minecraft/client/Minecraft.js index 930731a8..3874688d 100644 --- a/src/js/net/minecraft/client/Minecraft.js +++ b/src/js/net/minecraft/client/Minecraft.js @@ -399,7 +399,7 @@ export default class Minecraft { this.player.swingArm(); } - + // Pick block if (button === 1) { if (hitResult != null) { @@ -431,33 +431,45 @@ export default class Minecraft { // Don't place blocks if the player is standing there if (!placedBoundingBox.intersects(this.player.boundingBox)) { - let typeId = this.player.inventory.getItemInSelectedSlot(); - - // Get previous block - let prevTypeId = this.world.getBlockAt(x, y, z); - - if (typeId !== 0 && prevTypeId !== typeId) { - // Place block - this.world.setBlockAt(x, y, z, typeId); - - // Swing player arm - this.player.swingArm(); - - // Handle block abilities + // Handle left mouse clicked in block + let GuiLeftMouseCliked; + if (!this.player.isSneaking()) { + let typeId = this.world.getBlockAt(hitResult.x, hitResult.y, hitResult.z); let block = Block.getById(typeId); - block.onBlockPlaced(this.world, x, y, z, hitResult.face); - - // Play sound - let sound = block.getSound(); - let soundName = sound.getStepSound(); - this.soundManager.playSound( - soundName, - hitResult.x + 0.5, - hitResult.y + 0.5, - hitResult.z + 0.5, - 1.0, - sound.getPitch() * 0.8 - ); + GuiLeftMouseCliked = block.getGuiLeftMouseCliked() + } + + if (GuiLeftMouseCliked) { + this.displayScreen(new GuiLeftMouseCliked(this.player)) + } else { + let typeId = this.player.inventory.getItemInSelectedSlot(); + + // Get previous block + let prevTypeId = this.world.getBlockAt(x, y, z); + + if (typeId !== 0 && prevTypeId !== typeId) { + // Place block + this.world.setBlockAt(x, y, z, typeId); + + // Swing player arm + this.player.swingArm(); + + // Handle block abilities + let block = Block.getById(typeId); + block.onBlockPlaced(this.world, x, y, z, hitResult.face); + + // Play sound + let sound = block.getSound(); + let soundName = sound.getStepSound(); + this.soundManager.playSound( + soundName, + hitResult.x + 0.5, + hitResult.y + 0.5, + hitResult.z + 0.5, + 1.0, + sound.getPitch() * 0.8 + ); + } } } } diff --git a/src/js/net/minecraft/client/gui/screens/container/GuiContainerCraftingTable.js b/src/js/net/minecraft/client/gui/screens/container/GuiContainerCraftingTable.js new file mode 100644 index 00000000..114890c1 --- /dev/null +++ b/src/js/net/minecraft/client/gui/screens/container/GuiContainerCraftingTable.js @@ -0,0 +1,50 @@ +import GuiContainer from "../GuiContainer.js"; +import ContainerCraftingTable from "../../../inventory/container/ContainerCraftingTable.js"; +import InventoryBasic from "../../../inventory/inventory/InventoryBasic.js"; + +export default class GuiContainerCraftingTable extends GuiContainer { + + static inventory = new InventoryBasic(); + + constructor(player) { + super(new ContainerCraftingTable(player)); + + this.inventoryWidth = 195; + this.inventoryHeight = 165; + } + + init() { + this.textureInventory = this.getTexture("gui/container/crafting_table.png"); + + super.init(); + } + + drawTitle(stack) { + this.drawString(stack, "Crafting Table", this.x + 8, this.y + 6, 0xff404040, false); + } + + drawInventoryBackground(stack) { + this.drawSprite( + stack, + this.textureInventory, + 0, + 0, + this.inventoryWidth, + this.inventoryHeight, + this.x, + this.y, + this.inventoryWidth, + this.inventoryHeight + ); + } + + keyTyped(key, character) { + if (key === this.minecraft.settings.keyOpenInventory) { + this.minecraft.displayScreen(null); + return true; + } + + return super.keyTyped(key, character); + } + +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/inventory/container/ContainerCraftingTable.js b/src/js/net/minecraft/client/inventory/container/ContainerCraftingTable.js new file mode 100644 index 00000000..0771a881 --- /dev/null +++ b/src/js/net/minecraft/client/inventory/container/ContainerCraftingTable.js @@ -0,0 +1,67 @@ +import Container from "../Container.js"; +import Slot from "../Slot.js"; +import Block from "../../world/block/Block.js"; +import GuiContainerCraftingTable from "../../gui/screens/container/GuiContainerCraftingTable.js"; + +export default class ContainerCraftingTable extends Container { + + constructor(player) { + super(); + + this.itemList = []; + + let playerInventory = player.inventory; + + // Add crafting table slots + for (let y = 0; y < 3; ++y) { + for (let x = 0; x < 3; ++x) { + this.addSlot(new Slot(playerInventory, (y * 3 + x) + 36, 30 + x * 18, 17 + y * 18)); + } + } + + // Add crafting result slot + this.addSlot(new Slot(playerInventory, 45, 124, 35)); + + // Add inventory slots + for (let y = 0; y < 3; ++y) { + for (let x = 0; x < 9; ++x) { + this.addSlot(new Slot(playerInventory, (y * 9 + x) + 9, 8 + x * 18, 84 + y * 18)); + } + } + + // Add player hotbar + for (let x = 0; x < 9; ++x) { + this.addSlot(new Slot(playerInventory, x, 8 + x * 18, 142)); + } + + this.initItems(); + this.scrollTo(0); + } + + scrollTo(scrollOffset) { + let xOffset = (this.itemList.length + 9 - 1) / 9 - 5; + let yOffset = Math.floor((scrollOffset * xOffset) + 0.5); + + if (yOffset < 0) { + yOffset = 0; + } + + for (let y = 0; y < 5; ++y) { + for (let x = 0; x < 9; ++x) { + let index = x + (y + yOffset) * 9; + + if (index >= 0 && index < this.itemList.length) { + GuiContainerCraftingTable.inventory.setItem(x + y * 9, this.itemList[index]); + } else { + GuiContainerCraftingTable.inventory.setItem(x + y * 9, null); + } + } + } + } + + initItems() { + Block.blocks.forEach((block) => { + this.itemList.push(block.getId()); + }); + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/client/world/block/Block.js b/src/js/net/minecraft/client/world/block/Block.js index 768ba9cc..de5b5e87 100644 --- a/src/js/net/minecraft/client/world/block/Block.js +++ b/src/js/net/minecraft/client/world/block/Block.js @@ -101,6 +101,10 @@ export default class Block { } + getGuiLeftMouseCliked() { + + } + collisionRayTrace(world, x, y, z, start, end) { start = start.addVector(-x, -y, -z); end = end.addVector(-x, -y, -z); diff --git a/src/js/net/minecraft/client/world/block/BlockRegistry.js b/src/js/net/minecraft/client/world/block/BlockRegistry.js index ce58eff6..d9b135ec 100644 --- a/src/js/net/minecraft/client/world/block/BlockRegistry.js +++ b/src/js/net/minecraft/client/world/block/BlockRegistry.js @@ -9,6 +9,7 @@ import BlockTorch from "./type/BlockTorch.js"; import Sound from "./sound/Sound.js"; import Block from "./Block.js"; import BlockWood from "./type/BlockWood.js"; +import BlockCraftingTable from "./type/BlockCraftingTable.js"; import BlockBedrock from "./type/BlockBedrock.js"; import BlockGlass from "./type/BlockGlass.js"; import SoundGlass from "./sound/SoundGlass.js"; @@ -33,6 +34,7 @@ export class BlockRegistry { BlockRegistry.DIRT = new BlockDirt(3, 2); BlockRegistry.COBBLE_STONE = new BlockCobblestone(4, 14); BlockRegistry.WOOD = new BlockWood(5, 10); + BlockRegistry.CRAFTING_TABLE = new BlockCraftingTable(6, 15); BlockRegistry.BEDROCK = new BlockBedrock(7, 11); BlockRegistry.GRAVEL = new BlockGravel(13, 13); BlockRegistry.LOG = new BlockLog(17, 4); diff --git a/src/js/net/minecraft/client/world/block/type/BlockCraftingTable.js b/src/js/net/minecraft/client/world/block/type/BlockCraftingTable.js new file mode 100644 index 00000000..2777f479 --- /dev/null +++ b/src/js/net/minecraft/client/world/block/type/BlockCraftingTable.js @@ -0,0 +1,32 @@ +import GuiContainerCraftingTable from "../../../gui/screens/container/GuiContainerCraftingTable.js"; +import EnumBlockFace from "../../../../util/EnumBlockFace.js"; +import Block from "../Block.js"; + +export default class BlockCraftingTable extends Block { + + constructor(id, textureSlotId) { + super(id, textureSlotId); + } + + getGuiLeftMouseCliked() { + return GuiContainerCraftingTable + } + + getParticleTextureFace() { + return EnumBlockFace.NORTH; + } + + getTextureForFace(face) { + switch (face) { + case EnumBlockFace.BOTTOM: + return this.textureSlotId + 3; + case EnumBlockFace.TOP: + return this.textureSlotId; + case EnumBlockFace.SOUTH: + case EnumBlockFace.NORTH: + return this.textureSlotId + 1; + default: + return this.textureSlotId + 2; + } + } +} \ No newline at end of file diff --git a/src/js/net/minecraft/nbt/tag/TagRegistry.js b/src/js/net/minecraft/nbt/tag/TagRegistry.js index 3c508a58..c01435ae 100644 --- a/src/js/net/minecraft/nbt/tag/TagRegistry.js +++ b/src/js/net/minecraft/nbt/tag/TagRegistry.js @@ -15,7 +15,7 @@ export default class TagRegistry { static idToTag = new Map(); static tagToId = new Map(); - static { + constructor() { TagRegistry.register(1, ByteTag); TagRegistry.register(2, ShortTag); TagRegistry.register(3, IntTag); @@ -58,4 +58,4 @@ export default class TagRegistry { return new clazz(tagName); } -} \ No newline at end of file +} diff --git a/src/resources/gui/container/crafting_table.png b/src/resources/gui/container/crafting_table.png new file mode 100644 index 00000000..faf54d6b Binary files /dev/null and b/src/resources/gui/container/crafting_table.png differ diff --git a/src/resources/terrain/terrain.png b/src/resources/terrain/terrain.png index ff8d6bdc..4eca1ee6 100644 Binary files a/src/resources/terrain/terrain.png and b/src/resources/terrain/terrain.png differ