Merge branch develop into 1.16 · feather-rs/feather@289217a · GitHub
Skip to content

Commit 289217a

Browse files
committed
Merge branch develop into 1.16
2 parents 0ed8c22 + 2a613d8 commit 289217a

9 files changed

Lines changed: 659 additions & 45 deletions

File tree

old/core/network/src/mctypes.rs

Lines changed: 418 additions & 0 deletions
Large diffs are not rendered by default.

old/server/commands/src/arguments.rs

Lines changed: 114 additions & 0 deletions

old/server/commands/src/impls.rs

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! The implementations of various commands.
22
3-
use crate::arguments::Coordinates;
43
use crate::{
5-
arguments::{EntitySelector, ItemArgument, ParsedGamemode, PositiveI32Argument, TextArgument},
4+
arguments::{
5+
Coordinates, EntitySelector, ItemArgument, ParsedGamemode, PositiveI32Argument,
6+
TextArgument, TimeArgument, TimeQueryInformation, TimeSpec,
7+
},
68
CommandCtx,
79
};
810
use feather_core::inventory::{Inventory, SlotIndex};
@@ -11,7 +13,7 @@ use feather_core::util::{Gamemode, Position};
1113
use feather_definitions::Item;
1214
use feather_server_types::{
1315
Ban, ChatEvent, ChatPosition, GamemodeUpdateEvent, InventoryUpdateEvent, MessageReceiver, Name,
14-
Player, ShutdownChannels, Teleported, WrappedBanInfo,
16+
Player, ShutdownChannels, Teleported, TimeUpdateEvent, WrappedBanInfo,
1517
};
1618
use feather_server_util::{name_to_uuid_offline, name_to_uuid_online};
1719
use fecs::{Entity, IntoQuery, Read, ResourcesProvider, World};
@@ -816,3 +818,62 @@ pub fn pardonip(ctx: &mut CommandCtx, ip: String) -> anyhow::Result<()> {
816818

817819
Ok(None)
818820
}
821+
822+
#[command(usage = "time query <info>")]
823+
pub fn time_query(ctx: &mut CommandCtx, info: TimeQueryInformation) -> anyhow::Result<()> {
824+
let time = match info {
825+
TimeQueryInformation::DayTime => ctx.game.time.time_of_day(),
826+
TimeQueryInformation::GameTime => ctx.game.time.world_age(),
827+
TimeQueryInformation::Day => ctx.game.time.days(),
828+
};
829+
830+
if let Some(mut sender_message_receiver) = ctx.world.try_get_mut::<MessageReceiver>(ctx.sender)
831+
{
832+
let message = Text::from(TextValue::translate_with(
833+
"commands.time.query",
834+
vec![Text::from(time.to_string())],
835+
));
836+
sender_message_receiver.send(message);
837+
}
838+
839+
Ok(None)
840+
}
841+
842+
#[command(usage = "time add <time>")]
843+
pub fn time_add(ctx: &mut CommandCtx, time: TimeArgument) -> anyhow::Result<()> {
844+
time_set(ctx, ctx.game.time.time_of_day() + time.0)
845+
}
846+
847+
#[command(usage = "time set <time>")]
848+
pub fn time_set_0(ctx: &mut CommandCtx, time: TimeArgument) -> anyhow::Result<()> {
849+
time_set(ctx, time.0)
850+
}
851+
852+
#[command(usage = "time set <time_spec>")]
853+
pub fn time_set_1(ctx: &mut CommandCtx, time_spec: TimeSpec) -> anyhow::Result<()> {
854+
time_set(
855+
ctx,
856+
match time_spec {
857+
TimeSpec::Day => 1_000,
858+
TimeSpec::Noon => 6_000,
859+
TimeSpec::Night => 13_000,
860+
TimeSpec::Midnight => 18_000,
861+
},
862+
)
863+
}
864+
865+
pub fn time_set(ctx: &mut CommandCtx, time: u64) -> anyhow::Result<Option<String>> {
866+
ctx.game
867+
.handle(&mut ctx.world, TimeUpdateEvent { new_time: time });
868+
869+
if let Some(mut sender_message_receiver) = ctx.world.try_get_mut::<MessageReceiver>(ctx.sender)
870+
{
871+
let message = Text::from(TextValue::translate_with(
872+
"commands.time.set",
873+
vec![Text::from(ctx.game.time.time_of_day().to_string())],
874+
));
875+
sender_message_receiver.send(message);
876+
}
877+
878+
Ok(None)
879+
}

old/server/commands/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ impl CommandState {
130130

131131
pardon,
132132
pardonip,
133+
134+
time_query,
135+
time_add,
136+
time_set_0,
137+
time_set_1,
133138
}
134139

135140
Self {

old/server/src/event_handlers.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,7 @@ pub fn build_event_handlers() -> EventHandlers {
104104
on_chest_open_increment_viewers,
105105

106106
on_chest_close_decrement_viewers,
107+
108+
on_time_update,
107109
}
108110
}

old/server/types/src/events.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,9 @@ pub struct ReleaseChunkRequest {
307307
pub struct LoadChunkRequest {
308308
pub chunk: ChunkPosition,
309309
}
310+
311+
/// Updates day time changes.
312+
#[derive(Copy, Clone, Debug)]
313+
pub struct TimeUpdateEvent {
314+
pub new_time: u64,
315+
}

old/server/types/src/game.rs

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rand::{Rng, SeedableRng};
1919
use smallvec::SmallVec;
2020
use std::cell::{RefCell, RefMut};
2121
use std::fmt::Display;
22-
use std::ops::{Deref, DerefMut};
22+
use std::ops::Deref;
2323
use std::sync::atomic::{AtomicU32, Ordering};
2424
use std::sync::Arc;
2525
use thread_local::CachedThreadLocal;
@@ -271,26 +271,15 @@ impl Game {
271271
return;
272272
}
273273

274-
let (should_kill, old_health, new_health) =
275-
if let Some(mut health) = world.try_get_mut::<Health>(entity) {
276-
let old_health = health.0;
277-
let should_kill = match health.0.checked_sub(damage) {
278-
Some(0) => true,
279-
None => {
280-
// below 0
281-
health.0 = 0;
282-
true
283-
}
284-
Some(new_health) => {
285-
health.0 = new_health;
286-
false
287-
}
288-
};
289-
let new_health = health.0;
290-
(should_kill, Some(old_health), new_health)
291-
} else {
292-
(false, None, 0)
293-
};
274+
let (old_health, new_health) = if let Some(mut health) = world.try_get_mut::<Health>(entity)
275+
{
276+
let old_health = health.0;
277+
let new_health = health.0.saturating_sub(damage);
278+
health.0 = new_health;
279+
(Some(old_health), new_health)
280+
} else {
281+
(None, 0)
282+
};
294283

295284
if let Some(old_health) = old_health {
296285
self.handle(
@@ -301,10 +290,10 @@ impl Game {
301290
entity,
302291
},
303292
);
304-
}
305293

306-
if should_kill {
307-
self.kill(entity, world);
294+
if new_health == 0 {
295+
self.kill(entity, world);
296+
}
308297
}
309298
}
310299

@@ -383,32 +372,37 @@ impl ChunkEntities {
383372

384373
/// The current time of the world.
385374
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
386-
pub struct Time(pub u64);
387-
388-
impl Deref for Time {
389-
type Target = u64;
375+
pub struct Time {
376+
game_time: u64,
377+
day_time: u64,
378+
}
390379

391-
fn deref(&self) -> &Self::Target {
392-
&self.0
380+
impl Time {
381+
/// Adds some time to the time of day.
382+
pub fn set_time_of_day(&mut self, new_time: u64) {
383+
self.day_time = new_time;
384+
self.day_time %= 24_000;
393385
}
394-
}
395386

396-
impl DerefMut for Time {
397-
fn deref_mut(&mut self) -> &mut Self::Target {
398-
&mut self.0
387+
/// Adds some time to world age.
388+
pub fn set_world_age(&mut self, new_time: u64) {
389+
self.game_time = new_time;
399390
}
400-
}
401391

402-
impl Time {
403-
/// Returns the time of day. This is calculated
404-
/// as `time.0 % 24_000`.
392+
/// Returns the time of day.
405393
pub fn time_of_day(self) -> u64 {
406-
self.0 % 24_000
394+
self.day_time
395+
}
396+
397+
/// Returns the days passed. This is calculated
398+
/// as `time.0 / 24_000`.
399+
pub fn days(self) -> u64 {
400+
self.game_time / 24_000
407401
}
408402

409403
/// Returns the age of the world in ticks. Equivalent to `time.0`.
410404
pub fn world_age(self) -> u64 {
411-
self.0
405+
self.game_time
412406
}
413407
}
414408

old/server/util/src/time.rs

Lines changed: 16 additions & 2 deletions

0 commit comments

Comments
 (0)