From 8e7b5be3d42e151aef2cfe48208209635a46e447 Mon Sep 17 00:00:00 2001 From: TheIllusiveC4 Date: Thu, 16 Aug 2018 16:14:34 -0400 Subject: [PATCH] New SleepingTimeCheckEvent to add yet another way to control sleeping. (#5013) --- .../entity/player/EntityPlayer.java.patch | 18 ++++ .../event/ForgeEventFactory.java | 13 +++ .../entity/player/SleepingTimeCheckEvent.java | 55 ++++++++++++ .../debug/gameplay/AnytimeSleepingTest.java | 88 +++++++++++++++++++ 4 files changed, 174 insertions(+) create mode 100644 src/main/java/net/minecraftforge/event/entity/player/SleepingTimeCheckEvent.java create mode 100644 src/test/java/net/minecraftforge/debug/gameplay/AnytimeSleepingTest.java diff --git a/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch b/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch index 6f61a08a2..6fa569a7e 100644 --- a/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch +++ b/patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch @@ -29,6 +29,15 @@ this.field_70145_X = this.func_175149_v(); if (this.func_175149_v()) +@@ -207,7 +215,7 @@ + { + this.func_70999_a(true, true, false); + } +- else if (this.field_70170_p.func_72935_r()) ++ else if (!net.minecraftforge.event.ForgeEventFactory.fireSleepingTimeCheck(this, this.field_71081_bT)) + { + this.func_70999_a(false, true, true); + } @@ -367,6 +375,7 @@ this.func_70105_a(f, f1); } @@ -335,6 +344,15 @@ if (!this.field_70170_p.field_72995_K) { +@@ -1456,7 +1547,7 @@ + return EntityPlayer.SleepResult.NOT_POSSIBLE_HERE; + } + +- if (this.field_70170_p.func_72935_r()) ++ if (!net.minecraftforge.event.ForgeEventFactory.fireSleepingTimeCheck(this, this.field_71081_bT)) + { + return EntityPlayer.SleepResult.NOT_POSSIBLE_NOW; + } @@ -1484,8 +1575,7 @@ this.func_192030_dh(); this.func_70105_a(0.2F, 0.2F); diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 1e0fd1c17..a92837432 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -114,6 +114,7 @@ import net.minecraftforge.event.entity.player.PlayerSetSpawnEvent; import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent; import net.minecraftforge.event.entity.player.PlayerWakeUpEvent; import net.minecraftforge.event.entity.player.SleepingLocationCheckEvent; +import net.minecraftforge.event.entity.player.SleepingTimeCheckEvent; import net.minecraftforge.event.entity.player.UseHoeEvent; import net.minecraftforge.event.furnace.FurnaceFuelBurnTimeEvent; import net.minecraftforge.event.terraingen.ChunkGeneratorEvent; @@ -682,6 +683,18 @@ public class ForgeEventFactory return canContinueSleep == Result.ALLOW; } + public static boolean fireSleepingTimeCheck(EntityPlayer player, BlockPos sleepingLocation) + { + SleepingTimeCheckEvent evt = new SleepingTimeCheckEvent(player, sleepingLocation); + MinecraftForge.EVENT_BUS.post(evt); + + Result canContinueSleep = evt.getResult(); + if (canContinueSleep == Result.DEFAULT) + return !player.world.isDaytime(); + else + return canContinueSleep == Result.ALLOW; + } + public static ActionResult onArrowNock(ItemStack item, World world, EntityPlayer player, EnumHand hand, boolean hasAmmo) { ArrowNockEvent event = new ArrowNockEvent(player, item, hand, world, hasAmmo); diff --git a/src/main/java/net/minecraftforge/event/entity/player/SleepingTimeCheckEvent.java b/src/main/java/net/minecraftforge/event/entity/player/SleepingTimeCheckEvent.java new file mode 100644 index 000000000..919205947 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/entity/player/SleepingTimeCheckEvent.java @@ -0,0 +1,55 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2018. + * + * 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.entity.player; + +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import net.minecraftforge.fml.common.eventhandler.Event.HasResult; + +/** + * This event is fired when the game checks if players can sleep at this time.
+ * Failing this check will cause sleeping players to wake up and prevent awake players from sleeping.
+ * + * This event has a result. {@link HasResult}
+ * + * setResult(ALLOW) informs game that player can sleep at this time.
+ * setResult(DEFAULT) causes game to check !{@link World#isDaytime()} instead. + */ +@HasResult +public class SleepingTimeCheckEvent extends PlayerEvent +{ + private final BlockPos sleepingLocation; + + public SleepingTimeCheckEvent(EntityPlayer player, BlockPos sleepingLocation) + { + super(player); + this.sleepingLocation = sleepingLocation; + } + + /** + * Note that the sleeping location may be an approximated one. + * @return The player's sleeping location. + */ + public BlockPos getSleepingLocation() + { + return sleepingLocation; + } +} diff --git a/src/test/java/net/minecraftforge/debug/gameplay/AnytimeSleepingTest.java b/src/test/java/net/minecraftforge/debug/gameplay/AnytimeSleepingTest.java new file mode 100644 index 000000000..ce76ffdf8 --- /dev/null +++ b/src/test/java/net/minecraftforge/debug/gameplay/AnytimeSleepingTest.java @@ -0,0 +1,88 @@ +/* + * Minecraft Forge + * Copyright (c) 2016-2018. + * + * 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.debug.gameplay; + +import net.minecraft.client.renderer.block.model.ModelResourceLocation; +import net.minecraft.creativetab.CreativeTabs; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.client.event.ModelRegistryEvent; +import net.minecraftforge.client.model.ModelLoader; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.RegistryEvent; +import net.minecraftforge.event.entity.player.SleepingTimeCheckEvent; +import net.minecraftforge.fml.common.Mod; +import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; +import net.minecraftforge.fml.common.eventhandler.Event; +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; +import net.minecraftforge.fml.common.registry.GameRegistry; + +@Mod(modid = AnytimeSleepingTest.MODID, name = "Anytime Sleeping Test", version = "0.0", acceptableRemoteVersions = "*") +public class AnytimeSleepingTest +{ + public static final String MODID = "anytimesleepingtest"; + @GameRegistry.ObjectHolder(ItemSleepCharm.NAME) + public static final Item SLEEP_CHARM = null; + + @Mod.EventHandler + public void preInit(FMLPreInitializationEvent evt) + { + MinecraftForge.EVENT_BUS.register(this); + } + + @SubscribeEvent + public void onCheckSleepTime(SleepingTimeCheckEvent evt) + { + EntityPlayer player = evt.getEntityPlayer(); + if (player.getHeldItemMainhand().getItem() instanceof ItemSleepCharm) + { + evt.setResult(Event.Result.ALLOW); + } + } + + @Mod.EventBusSubscriber(modid = MODID) + public static class Registration + { + @SubscribeEvent + public static void registerItems(RegistryEvent.Register evt) + { + evt.getRegistry().register(new ItemSleepCharm()); + } + + @SubscribeEvent + public static void registerModels(ModelRegistryEvent evt) + { + ModelLoader.setCustomModelResourceLocation(SLEEP_CHARM, 0, new ModelResourceLocation("minecraft:totem", "inventory")); + } + } + + public static class ItemSleepCharm extends Item + { + static final String NAME = "sleep_charm"; + + private ItemSleepCharm() + { + this.setCreativeTab(CreativeTabs.MISC); + this.setUnlocalizedName(MODID + ":" + NAME); + this.setRegistryName(new ResourceLocation(MODID, NAME)); + } + } +}