less jank

master
Drake 1 year ago
parent 411718d8db
commit d7266bb3b8

1
Cargo.lock generated

@ -1875,6 +1875,7 @@ dependencies = [
"anyhow",
"flume",
"once_cell",
"rand",
"tracing",
"tracing-subscriber",
"valence",

@ -14,3 +14,4 @@ valence_anvil = { git = "https://github.com/valence-rs/valence" }
flume = "0.10.14"
anyhow = "1.0.70"
once_cell = "1.17.1"
rand = "0.8.5"

@ -3,6 +3,7 @@ mod chests;
mod commands;
mod player;
mod special;
mod utils;
mod world;
use std::net::SocketAddr;

@ -3,7 +3,9 @@ use std::{
sync::{atomic::Ordering, Mutex},
};
use crate::utils::get_death_message;
use once_cell::sync::Lazy;
use valence::bevy_ecs;
use valence::{
entity::{living::Health, player::PlayerEntityBundle, EntityId},
packet::WritePacket,
@ -20,10 +22,11 @@ use valence_protocol::{
use crate::PLAYER_COUNT;
static PLAYER_HEIGHT_HISTORY: Lazy<Mutex<HashMap<i32, f64>>> =
Lazy::new(|| Mutex::new(HashMap::new()));
#[derive(Component, Debug)]
pub struct OldHeight(pub f64);
static PLAYER_DEATHS: Lazy<Mutex<HashMap<Uuid, bool>>> = Lazy::new(|| Mutex::new(HashMap::new()));
#[derive(Component, Debug)]
pub struct Alive(pub bool);
pub fn init_clients(
mut new_clients: Query<
@ -47,16 +50,20 @@ pub fn init_clients(
// initialize client
has_respawn_screen.0 = true;
*game_mode = GameMode::Creative;
commands.entity(entity).insert(PlayerEntityBundle {
location: Location(instances.single()),
position: Position(DVec3 {
x: -214.0,
y: 83.0,
z: 276.0,
}),
uuid: *uuid,
..Default::default()
});
commands.entity(entity).insert((
OldHeight(-1.0),
Alive(true),
PlayerEntityBundle {
location: Location(instances.single()),
position: Position(DVec3 {
x: -214.0,
y: 83.0,
z: 276.0,
}),
uuid: *uuid,
..Default::default()
},
));
// send packet that magically fixes chunks
// TODO: make this configurable as different maps will have different chunks we need to force load
@ -105,17 +112,19 @@ pub fn fall_damage(
(
&mut Client,
&OnGround,
&EntityId,
&mut Health,
&GameMode,
&Position,
&mut OldHeight,
),
Changed<Position>,
>,
instances: Query<&Instance>,
) {
let instance = instances.single();
for (_client, on_ground, id, mut health, gamemode, position) in &mut clients {
for (_client, on_ground, mut health, gamemode, position, mut historical_position) in
&mut clients
{
let current_block = instance
.block(BlockPos {
x: position.0.x.floor() as i32,
@ -123,51 +132,46 @@ pub fn fall_damage(
z: position.0.z.floor() as i32,
})
.unwrap();
let mut hashmap = PLAYER_HEIGHT_HISTORY.lock().unwrap();
if *gamemode == GameMode::Survival {
// hack that potentially makes water not apply fall damage?
if current_block.state().to_kind() == BlockKind::Vine
|| current_block.state().to_kind() == BlockKind::SlimeBlock
|| current_block.state().is_liquid()
{
hashmap.insert(id.get(), position.0.y);
historical_position.0 = position.0.y;
}
let old_position = hashmap.get(&id.get()).unwrap_or(&-1.0);
let delta_y = old_position - position.0.y;
let delta_y = historical_position.0 - position.0.y;
if on_ground.0 {
if delta_y > 3.0 {
health.0 -= (delta_y - 3.0) as f32;
}
hashmap.insert(id.get(), position.0.y);
historical_position.0 = position.0.y;
} else if delta_y < 0.0 {
hashmap.insert(id.get(), position.0.y);
historical_position.0 = position.0.y;
}
}
}
}
pub fn death(mut clients: Query<(&mut Client, &UniqueId, &Health, &Username)>) {
pub fn death(mut clients: Query<(&mut Client, &UniqueId, &Health, &Username, &mut Alive)>) {
let mut messages: Vec<Text> = vec![];
for (mut client, id, health, username) in &mut clients {
let mut hashmap = PLAYER_DEATHS.lock().unwrap();
let is_player_dead = hashmap.get(&id.0).unwrap_or(&false);
if health.0 <= 0.0 && !is_player_dead {
for (mut client, id, health, username, mut is_alive) in &mut clients {
if health.0 <= 0.0 && is_alive.0 {
tracing::debug!("Trying to kill player {}", username);
client.kill(None, format!("{} fell off.", username));
hashmap.insert(id.0, true);
messages.push(
format!("{} fell off.", username)
.italic()
.color(Color::GRAY),
);
let death_message = get_death_message(username.to_string());
client.kill(None, death_message.clone());
is_alive.0 = false;
messages.push(death_message.italic().color(Color::GRAY));
}
}
for (mut client, _, _, _) in &mut clients {
for (mut client, _, _, _, _) in &mut clients {
for message in messages.iter() {
client.send_message(message.clone());
}
@ -193,14 +197,15 @@ pub fn respawn(
&mut Location,
&UniqueId,
&mut Health,
&mut Alive,
)>,
mut events: EventReader<PerformRespawn>,
instances: Query<Entity, With<Instance>>,
) {
for event in events.iter() {
if let Ok((mut pos, mut look, mut loc, uuid, mut health)) = clients.get_mut(event.client) {
let mut hashmap = PLAYER_DEATHS.lock().unwrap();
if let Ok((mut pos, mut look, mut loc, uuid, mut health, mut is_alive)) =
clients.get_mut(event.client)
{
pos.set(DVec3 {
x: -214.0,
y: 83.0,
@ -211,7 +216,7 @@ pub fn respawn(
loc.0 = instances.single();
health.0 = 20.0;
hashmap.insert(uuid.0, false);
is_alive.0 = true;
}
}
}

@ -0,0 +1,34 @@
use rand::prelude::*;
static DEATH_MESSAGES: [&str; 21] = [
"{username} got fired by Mike Mood.",
"{username} let the water drip on their springlocks.",
"{username} got stuck on a fetch quest.",
"{username} SIGSEGVed.",
"{username} was so engrossed in their xReader fanfic that they didn't notice the player sneaking up behind them.",
"{username} thought the Ink Demon would not kill them and actually love them. They were wrong.",
"{username} ignored all of the warning signs and went on to die an entirely avoidable death.",
"{username} thought they could out-drink Husk. They were wrong.",
"{username} tried to outfuck Angel Dust.",
"{username} tried to outfuck Ozzie.",
"{username} tripped over a banana man and fell into a ravine.",
"{username} overdosed on too much Good & Evil.",
"{username} got hidden in (6 feet of) the sand.",
"{username} failed harder than any American protest.",
"{username} failed to dodge Mitch McConnell's filibuster.",
"{username} got run over by the big red Brexit bus.",
"{username} fell out of a 4th story window.",
"{username} got lost in a Wal-Mart and starved to death.",
"{username} failed to feed their Discord kittens on time and suffered the consequences.",
"{username} got their heart stolen by a blind pickpocket.",
"{username} forgot both themselves and 2012."
];
pub fn get_death_message(username: String) -> String {
let mut rng = thread_rng();
return DEATH_MESSAGES
.choose(&mut rng)
.unwrap()
.replace("{username}", &username)
.to_string();
}
Loading…
Cancel
Save