fix: Long night event was triggered multiple times
chore: optimized LongNightEvent.isRunning
This commit is contained in:
2 files changed
+5
-3
No files matched your search
@@ -7,7 +7,7 @@ import net.minecraft.world.World;
|
|||||||
|
|
||||||
public class SFRandomEventRegistry extends SimpleRandomEventRegistry {
|
public class SFRandomEventRegistry extends SimpleRandomEventRegistry {
|
||||||
public SFRandomEventRegistry() {
|
public SFRandomEventRegistry() {
|
||||||
registerEvent(LongNightEvent::new, World.OVERWORLD, world -> world.getTimeOfDay() % 18000 == 0); // At midnight
|
registerEvent(LongNightEvent::new, World.OVERWORLD, world -> !LongNightEvent.isRunning() && world.getTimeOfDay() % 18000 == 0); // At midnight
|
||||||
registerEvent(DawnAfterLongNightEvent::new, World.OVERWORLD, world -> world.getTimeOfDay() % 22200 == 0); // At dawn
|
registerEvent(DawnAfterLongNightEvent::new, World.OVERWORLD, world -> world.getTimeOfDay() % 22200 == 0); // At dawn
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -11,12 +11,14 @@ import net.minecraft.world.World;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class LongNightEvent extends RandomEvent {
|
public class LongNightEvent extends RandomEvent {
|
||||||
static boolean justExpirencedLongNight;
|
static boolean justExpirencedLongNight;
|
||||||
|
static boolean isAtLongNight;
|
||||||
private final World world;
|
private final World world;
|
||||||
private boolean doDaylightCycle;
|
private boolean doDaylightCycle;
|
||||||
private boolean doTraderSpawning;
|
private boolean doTraderSpawning;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int start() {
|
public int start() {
|
||||||
|
isAtLongNight = true;
|
||||||
world.getServer().getPlayerManager().broadcast(Text.literal("月亮停滞在空中...").withColor(Colors.BLUE), false);
|
world.getServer().getPlayerManager().broadcast(Text.literal("月亮停滞在空中...").withColor(Colors.BLUE), false);
|
||||||
this.doDaylightCycle = world.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE);
|
this.doDaylightCycle = world.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE);
|
||||||
this.doTraderSpawning = world.getGameRules().getBoolean(GameRules.DO_TRADER_SPAWNING);
|
this.doTraderSpawning = world.getGameRules().getBoolean(GameRules.DO_TRADER_SPAWNING);
|
||||||
@@ -31,13 +33,13 @@ public class LongNightEvent extends RandomEvent {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void end() {
|
public void end() {
|
||||||
|
isAtLongNight = false;
|
||||||
justExpirencedLongNight = true;
|
justExpirencedLongNight = true;
|
||||||
world.getGameRules().get(GameRules.DO_DAYLIGHT_CYCLE).set(doDaylightCycle, world.getServer());
|
world.getGameRules().get(GameRules.DO_DAYLIGHT_CYCLE).set(doDaylightCycle, world.getServer());
|
||||||
world.getGameRules().get(GameRules.DO_TRADER_SPAWNING).set(doTraderSpawning, world.getServer());
|
world.getGameRules().get(GameRules.DO_TRADER_SPAWNING).set(doTraderSpawning, world.getServer());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static boolean isRunning() {
|
public static boolean isRunning() {
|
||||||
return LongNightEvent.justExpirencedLongNight || SFCraft.getInstance().getRandomEventRegistry()
|
return isAtLongNight;
|
||||||
.getActiveEvents().stream().anyMatch(it -> it.getEvent().getClass() == LongNightEvent.class); // faster than instanceof
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user