add: query LongNightEvent progress by clock (#9)
This commit is contained in:
2 files changed
+22
-3
No files matched your search
@@ -7,10 +7,13 @@ import io.ib67.sfcraft.callback.SFCallbacks;
|
||||
import io.ib67.sfcraft.module.randomevt.longnight.DawnAfterLongNightEvent;
|
||||
import io.ib67.sfcraft.module.randomevt.longnight.LongNightEvent;
|
||||
import io.ib67.sfcraft.registry.event.RandomEventRegistry;
|
||||
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
|
||||
import net.fabricmc.fabric.api.event.player.UseItemCallback;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.Items;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Colors;
|
||||
import net.minecraft.util.Unit;
|
||||
import net.minecraft.util.*;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
@@ -28,6 +31,16 @@ public class LongNightModule extends ServerModule {
|
||||
registry.registerEvent(DawnAfterLongNightEvent::new, World.OVERWORLD
|
||||
, world -> world.getTimeOfDay() % 22200 == 0); // At dawn
|
||||
SFCallbacks.PLAYER_SLEEP.register(this::onPlayerSleep);
|
||||
UseItemCallback.EVENT.register(this::onUseClock);
|
||||
}
|
||||
|
||||
private TypedActionResult<ItemStack> onUseClock(PlayerEntity player, World world, Hand hand) {
|
||||
if (player.isSpectator()) return TypedActionResult.pass(ItemStack.EMPTY);
|
||||
if (player.getStackInHand(hand).isOf(Items.CLOCK)) {
|
||||
if (LongNightEvent.isRunning())
|
||||
player.sendMessage(Text.literal("永夜还将持续约 " + LongNightEvent.getRemainingTicks() / 20 + " 秒。").withColor(Colors.LIGHT_GRAY));
|
||||
}
|
||||
return TypedActionResult.pass(ItemStack.EMPTY);
|
||||
}
|
||||
|
||||
public Either<PlayerEntity.SleepFailureReason, Unit> onPlayerSleep(PlayerEntity player, BlockPos pos) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.ib67.sfcraft.module.randomevt.longnight;
|
||||
|
||||
import io.ib67.sfcraft.module.randomevt.RandomEvent;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import net.minecraft.text.Text;
|
||||
import net.minecraft.util.Colors;
|
||||
@@ -11,6 +12,8 @@ import net.minecraft.world.World;
|
||||
public class LongNightEvent extends RandomEvent {
|
||||
static boolean justExpirencedLongNight;
|
||||
static boolean isAtLongNight;
|
||||
@Getter
|
||||
private static int remainingTicks;
|
||||
private final World world;
|
||||
private boolean doDaylightCycle;
|
||||
private boolean doTraderSpawning;
|
||||
@@ -24,17 +27,20 @@ public class LongNightEvent extends RandomEvent {
|
||||
this.doTraderSpawning = world.getGameRules().getBoolean(GameRules.DO_TRADER_SPAWNING);
|
||||
world.getGameRules().get(GameRules.DO_DAYLIGHT_CYCLE).set(false, world.getServer());
|
||||
world.getGameRules().get(GameRules.DO_TRADER_SPAWNING).set(false, world.getServer());
|
||||
return world.random.nextBetween(8400, 2 * 8400);
|
||||
remainingTicks = world.random.nextBetween(8400, 2 * 8400);
|
||||
return remainingTicks;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(int ticks) {
|
||||
remainingTicks--;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void end() {
|
||||
isAtLongNight = false;
|
||||
justExpirencedLongNight = true;
|
||||
remainingTicks = 0;
|
||||
world.getGameRules().get(GameRules.DO_DAYLIGHT_CYCLE).set(doDaylightCycle, world.getServer());
|
||||
world.getGameRules().get(GameRules.DO_TRADER_SPAWNING).set(doTraderSpawning, world.getServer());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user