Skip to content

Fix loading error #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/js/Start.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
66 changes: 39 additions & 27 deletions src/js/net/minecraft/client/Minecraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export default class Minecraft {

this.player.swingArm();
}

// Pick block
if (button === 1) {
if (hitResult != null) {
Expand Down Expand Up @@ -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
);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
@@ -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());
});
}
}
4 changes: 4 additions & 0 deletions src/js/net/minecraft/client/world/block/Block.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/js/net/minecraft/client/world/block/BlockRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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);
Expand Down
32 changes: 32 additions & 0 deletions src/js/net/minecraft/client/world/block/type/BlockCraftingTable.js
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
4 changes: 2 additions & 2 deletions src/js/net/minecraft/nbt/tag/TagRegistry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -58,4 +58,4 @@ export default class TagRegistry {
return new clazz(tagName);
}

}
}
Binary file added src/resources/gui/container/crafting_table.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified src/resources/terrain/terrain.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.