Skip to content

Commit 0a476ea

Browse files
committed
Add module player-velocity
1 parent 06103f0 commit 0a476ea

3 files changed

Lines changed: 66 additions & 0 deletions

File tree

scripts/player-velocity/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 JaylyDev
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

scripts/player-velocity/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Vector, ExplosionOptions, MinecraftEffectTypes, Player, EntityHealthComponent } from "mojang-minecraft";
2+
3+
/**
4+
* @remarks
5+
* Sets a velocity for the entity to move with.
6+
* Fixes GameTest native player.setVelocity
7+
* @param {Vector} velocity
8+
* @param {Player} player
9+
* @throws This function can throw errors.
10+
*/
11+
export function setVelocity (velocity, player) {
12+
if (!(player instanceof Player)) throw TypeError("Native type conversion failed.");
13+
14+
velocity = Vector.add(velocity, new Vector(0, -1, 0));
15+
/** @type {EntityHealthComponent} */
16+
// @ts-ignore
17+
const health = player.getComponent("health");
18+
19+
// log current hp and add instant health to prevent explosion view tension
20+
const currentHp = health.current;
21+
player.addEffect(MinecraftEffectTypes.instantHealth, 0, 255);
22+
23+
const explosion = new ExplosionOptions();
24+
explosion.breaksBlocks = false;
25+
26+
// create explosion & set velocity
27+
player.setVelocity(velocity);
28+
player.dimension.createExplosion(player.location, 0.001, explosion);
29+
player.runCommand("stopsound @s random.explode");
30+
31+
// set health back to previous value before instant health
32+
health.setCurrent(currentHp);
33+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { MinecraftItemTypes, Player, Vector, world } from "mojang-minecraft";
2+
import { setVelocity } from "./index.js";
3+
4+
world.events.beforeItemUse.subscribe((event) => {
5+
if (event.item.id !== MinecraftItemTypes.feather.id) return;
6+
if (!(event.source instanceof Player)) return;
7+
8+
var velocity = Vector.multiply(event.source.viewVector, new Vector(1.5, 2, 1.5));
9+
10+
setVelocity(velocity, event.source);
11+
event.source.runCommand("playsound armor.equip_leather @s");
12+
});

0 commit comments

Comments
 (0)