From f9686cf0988d4b1cc3c63f890c7270e3bf745912 Mon Sep 17 00:00:00 2001 From: Paul Becker Date: Mon, 2 Dec 2019 23:08:14 +0100 Subject: [PATCH] New SleepFinishedTimeEvent allowing control of world time when waking up. (#6043) --- .../world/server/ServerWorld.java.patch | 6 +- .../event/ForgeEventFactory.java | 12 ++-- .../event/world/SleepFinishedTimeEvent.java | 61 +++++++++++++++++++ 3 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/minecraftforge/event/world/SleepFinishedTimeEvent.java diff --git a/patches/minecraft/net/minecraft/world/server/ServerWorld.java.patch b/patches/minecraft/net/minecraft/world/server/ServerWorld.java.patch index fd1267eae..3db739d5e 100644 --- a/patches/minecraft/net/minecraft/world/server/ServerWorld.java.patch +++ b/patches/minecraft/net/minecraft/world/server/ServerWorld.java.patch @@ -73,15 +73,17 @@ } if (this.func_72912_H().func_76093_s() && this.func_175659_aa() != Difficulty.HARD) { -@@ -280,7 +291,7 @@ +@@ -280,8 +291,8 @@ })) { this.field_73068_P = false; if (this.func_82736_K().func_223586_b(GameRules.field_223607_j)) { - long l = this.field_72986_A.func_76073_f() + 24000L; +- this.func_72877_b(l - l % 24000L); + long l = this.func_72820_D() + 24000L; - this.func_72877_b(l - l % 24000L); ++ this.func_72877_b(net.minecraftforge.event.ForgeEventFactory.onSleepFinished(this, l - l % 24000L, this.func_72820_D())); } + this.field_217491_A.stream().filter(LivingEntity::func_70608_bn).forEach((p_217444_0_) -> { @@ -304,6 +315,7 @@ iprofiler.func_219895_b("portalForcer"); diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 99bddd365..62883a6a1 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -33,7 +33,6 @@ import net.minecraft.entity.MobEntity; import net.minecraft.entity.SpawnReason; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.EntityClassification; -import net.minecraft.entity.merchant.IMerchant; import net.minecraft.entity.effect.LightningBoltEntity; import net.minecraft.entity.item.ItemEntity; import net.minecraft.entity.monster.ZombieEntity; @@ -49,7 +48,6 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUseContext; import net.minecraft.world.spawner.AbstractSpawner; import net.minecraft.util.ActionResult; -import net.minecraft.util.DamageSource; import net.minecraft.util.ActionResultType; import net.minecraft.util.Direction; import net.minecraft.util.Hand; @@ -70,8 +68,6 @@ import net.minecraft.world.World; import net.minecraft.world.server.ServerWorld; import net.minecraft.world.WorldSettings; import net.minecraft.world.biome.Biome; -import net.minecraft.world.chunk.IChunk; -import net.minecraft.world.gen.ChunkGenerator; import net.minecraft.world.storage.IPlayerFileData; import net.minecraft.world.storage.SaveHandler; import net.minecraft.world.storage.loot.LootTable; @@ -128,6 +124,7 @@ import net.minecraftforge.event.world.ExplosionEvent; import net.minecraftforge.event.world.GetCollisionBoxesEvent; import net.minecraftforge.event.world.PistonEvent; import net.minecraftforge.event.world.SaplingGrowTreeEvent; +import net.minecraftforge.event.world.SleepFinishedTimeEvent; import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.eventbus.api.Event; import net.minecraftforge.eventbus.api.Event.Result; @@ -699,4 +696,11 @@ public class ForgeEventFactory { return MinecraftForge.EVENT_BUS.post(new PistonEvent.Post(world, pos, direction, extending ? PistonEvent.PistonMoveType.EXTEND : PistonEvent.PistonMoveType.RETRACT)); } + + public static long onSleepFinished(ServerWorld world, long newTime, long minTime) + { + SleepFinishedTimeEvent event = new SleepFinishedTimeEvent(world, newTime, minTime); + MinecraftForge.EVENT_BUS.post(event); + return event.getNewTime(); + } } diff --git a/src/main/java/net/minecraftforge/event/world/SleepFinishedTimeEvent.java b/src/main/java/net/minecraftforge/event/world/SleepFinishedTimeEvent.java new file mode 100644 index 000000000..673065560 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/world/SleepFinishedTimeEvent.java @@ -0,0 +1,61 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2019. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.event.world; + +import net.minecraft.world.server.ServerWorld; + +/** + * This event is fired when all players are asleep and the time should be set to day.
+ * + * setWakeUpTime(wakeUpTime) sets a new time that will be added to the dayTime.
+ */ +public class SleepFinishedTimeEvent extends WorldEvent +{ + private long newTime; + private final long minTime; + + public SleepFinishedTimeEvent(ServerWorld worldIn, long newTimeIn, long minTimeIn) + { + super(worldIn); + this.newTime = newTimeIn; + this.minTime = minTimeIn; + } + + /** + * @return the new time + */ + public long getNewTime() + { + return newTime; + } + + /** + * Sets the new time which should be set when all players wake up + * @param newTimeIn The new time at wakeup + * @return {@code false} if newTimeIn was lower than current time + */ + public boolean setTimeAddition(long newTimeIn) + { + if (minTime > newTimeIn) + return false; + this.newTime = newTimeIn; + return true; + } +}