Skip to content

Commit 30abb94

Browse files
authored
Merge pull request michaljaz#29 from Picoseconds/threejs-javascript
Lightbulb pull request
2 parents b28d84b + 8a7d033 commit 30abb94

File tree

4 files changed

+87
-26
lines changed

4 files changed

+87
-26
lines changed

src/client/scripts/EventHandler.js

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,23 @@ class EventHandler {
108108
_this.gameState === "gameLock"
109109
) {
110110
_this.game.socket.emit("move", _this.controls[z.code], true);
111-
if (_this.controls[z.code] === "sprint") {
112-
var to = {
113-
fov: _this.game.fov + 10,
114-
};
115-
new TWEEN.Tween(_this.game.camera)
116-
.to(to, 200)
117-
.easing(TWEEN.Easing.Quadratic.Out)
118-
.onUpdate(function () {
119-
return _this.game.camera.updateProjectionMatrix();
120-
})
121-
.start();
111+
switch (_this.controls[z.code]) {
112+
case "sprint":
113+
var to = {
114+
fov: _this.game.fov,
115+
};
116+
new TWEEN.Tween(_this.game.camera)
117+
.to(to, 200)
118+
.easing(TWEEN.Easing.Quadratic.Out)
119+
.onUpdate(function () {
120+
return _this.game.camera.updateProjectionMatrix();
121+
})
122+
.start();
123+
break;
124+
125+
case "sneak":
126+
_this.game.headHeight = 16.7;
127+
break;
122128
}
123129
}
124130
});
@@ -127,17 +133,23 @@ class EventHandler {
127133
delete _this.keys[z.code];
128134
if (_this.controls[z.code] !== undefined) {
129135
_this.game.socket.emit("move", _this.controls[z.code], false);
130-
if (_this.controls[z.code] === "sprint") {
131-
var to = {
132-
fov: _this.game.fov,
133-
};
134-
new TWEEN.Tween(_this.game.camera)
135-
.to(to, 200)
136-
.easing(TWEEN.Easing.Quadratic.Out)
137-
.onUpdate(function () {
138-
return _this.game.camera.updateProjectionMatrix();
139-
})
140-
.start();
136+
switch (_this.controls[z.code]) {
137+
case "sprint":
138+
var to = {
139+
fov: _this.game.fov,
140+
};
141+
new TWEEN.Tween(_this.game.camera)
142+
.to(to, 200)
143+
.easing(TWEEN.Easing.Quadratic.Out)
144+
.onUpdate(function () {
145+
return _this.game.camera.updateProjectionMatrix();
146+
})
147+
.start();
148+
break;
149+
150+
case "sneak":
151+
_this.game.headHeight = 17;
152+
break;
141153
}
142154
}
143155
});

src/client/scripts/InventoryBar.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ var InventoryBar = class InventoryBar {
1414
}
1515
}
1616

17+
setGamemode(gamemode) {
18+
// mineflayer doesn't currently include support for spectator mode
19+
if (gamemode === "creative") {
20+
$((gamemode === "spectator" ? ".inv_bar, .inv_cursor" : "") + ".player_hp, .player_food, .xp_bar_empty, xp_bar").css("display", "none");
21+
} else {
22+
$(".player_hp, .player_food, .xp_bar_empty, xp_bar").css("display", "initial");
23+
}
24+
}
25+
1726
setHp(points) {
1827
var lista = {};
1928
for (let i = 1; i <= 10; i++) {

src/client/scripts/index.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ import { BlockPlace } from "./BlockPlace.js";
1616
import { DistanceBasedFog } from "./DistanceBasedFog.js";
1717
import { EventHandler } from "./EventHandler.js";
1818

19+
const dimNamesInt = {
20+
"-1": "minecraft:nether",
21+
0: "minecraft:overworld",
22+
1: "minecraft:end",
23+
};
24+
1925
class Game {
2026
constructor() {
2127
var _this = this;
@@ -49,6 +55,9 @@ class Game {
4955
"minecraft:overworld": [165 / 255, 192 / 255, 254 / 255],
5056
"minecraft:the_end": [1 / 255, 20 / 255, 51 / 255],
5157
"minecraft:the_nether": [133 / 255, 40 / 255, 15 / 255],
58+
59+
"minecraft:end": [1 / 255, 20 / 255, 51 / 255],
60+
"minecraft:nether": [133 / 255, 40 / 255, 15 / 255],
5261
};
5362
this.camera = new THREE.PerspectiveCamera(this.fov, 2, 0.1, 1000);
5463
this.camera.rotation.order = "YXZ";
@@ -75,6 +84,7 @@ class Game {
7584
);
7685
this.stats.showPanel(0);
7786
document.body.appendChild(this.stats.dom);
87+
this.headHeight = 17;
7888
this.pii = new PlayerInInventory(this);
7989
this.bb = new BlockBreak(this);
8090
this.bp = new BlockPlace(this);
@@ -101,12 +111,25 @@ class Game {
101111
_this.camera.rotation.y = yaw;
102112
_this.camera.rotation.x = pitch;
103113
});
104-
this.socket.on("dimension", function (dim) {
114+
this.socket.on("dimension", function (dim, format) {
115+
switch (format) {
116+
case "int":
117+
dim = dimNamesInt[dim];
118+
break;
119+
120+
case "world":
121+
// idk what this is yet
122+
break;
123+
}
124+
105125
_this.dimension = dim;
106126
console.log(`Player dimension has been changed: ${dim}`);
107127
_this.world.resetWorld();
128+
108129
var bg = _this.dimBg[dim];
109130
if (bg === undefined) {
131+
bg = _this.dimBg["minecraft:overworld"];
132+
110133
_this.scene.background = new THREE.Color(
111134
..._this.dimBg["minecraft:overworld"]
112135
);
@@ -122,6 +145,9 @@ class Game {
122145
this.socket.on("mapChunk", function (sections, x, z) {
123146
_this.world.computeSections(sections, x, z);
124147
});
148+
this.socket.on("game", function (gameData) {
149+
_this.inv_bar.setGamemode(gameData.gameMode);
150+
});
125151
this.socket.on("hp", function (points) {
126152
_this.inv_bar.setHp(points);
127153
});
@@ -135,15 +161,17 @@ class Game {
135161
_this.chat.log(msg);
136162
});
137163
this.socket.on("kicked", function (reason) {
138-
_this.chat.log("You have been kicked! Reason: " + JSON.parse(reason).text);
164+
_this.chat.log(
165+
"You have been kicked! Reason: " + JSON.parse(reason).text
166+
);
139167
});
140168
this.socket.on("xp", function (xp) {
141169
_this.inv_bar.setXp(xp.level, xp.progress);
142170
});
143171
this.socket.on("move", function (pos) {
144172
var to = {
145173
x: pos.x - 0.5,
146-
y: pos.y + 17,
174+
y: pos.y + _this.headHeight,
147175
z: pos.z - 0.5,
148176
};
149177
new TWEEN.Tween(_this.camera.position)

src/index.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ io.sockets.on("connection", function (socket) {
5454
socket.emit("mapChunk", cell.sections, packet.x, packet.z);
5555
});
5656
bot._client.on("respawn", function (packet) {
57-
socket.emit("dimension", packet.dimension.value.effects.value);
57+
socket.emit(
58+
"dimension",
59+
packet.dimension,
60+
bot.supportFeature("dimensionIsAWorld")
61+
? "world"
62+
: bot.supportFeature("dimensionIsAString")
63+
? "string"
64+
: "int"
65+
);
5866
});
5967
bot.on("heldItemChanged", function (item) {
6068
heldItem = item;
@@ -95,6 +103,9 @@ io.sockets.on("connection", function (socket) {
95103
bot.on("diggingAborted", function (block) {
96104
socket.emit("diggingAborted", block);
97105
});
106+
bot.on("game", function () {
107+
socket.emit("game", bot.game);
108+
});
98109
var inv = "";
99110
var interval = setInterval(function () {
100111
var inv_new = JSON.stringify(bot.inventory.slots);
@@ -153,6 +164,7 @@ io.sockets.on("connection", function (socket) {
153164
} else if (state === "left") {
154165
state = "right";
155166
}
167+
156168
bot.setControlState(state, toggle);
157169
});
158170
socket.on("command", function (com) {

0 commit comments

Comments
 (0)