Skip to content

Commit fae5ff6

Browse files
committed
Allow sleeping without bed
1 parent accf6cf commit fae5ff6

File tree

4 files changed

+188
-17
lines changed

4 files changed

+188
-17
lines changed

patches/minecraft/net/minecraft/entity/player/EntityPlayer.java.patch

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,12 @@
296296
if (!this.field_70170_p.field_72995_K)
297297
{
298298
if (this.func_70608_bn() || !this.func_70089_S())
299-
@@ -1352,7 +1440,7 @@
299+
@@ -1350,9 +1438,9 @@
300300

301-
if (this.field_70170_p.func_175667_e(p_180469_1_))
301+
this.func_70105_a(0.2F, 0.2F);
302+
303+
- if (this.field_70170_p.func_175667_e(p_180469_1_))
304+
+ if (this.field_70170_p.func_175667_e(p_180469_1_) && field_70170_p.func_180495_p(p_180469_1_).func_177230_c().isBed(field_70170_p, p_180469_1_, this))
302305
{
303306
- EnumFacing enumfacing = (EnumFacing)this.field_70170_p.func_180495_p(p_180469_1_).func_177229_b(BlockDirectional.field_176387_N);
304307
+ EnumFacing enumfacing = this.field_70170_p.func_180495_p(p_180469_1_).func_177230_c().getBedDirection(field_70170_p, p_180469_1_);
@@ -323,12 +326,23 @@
323326

324327
if (blockpos == null)
325328
{
326-
@@ -1448,14 +1537,14 @@
329+
@@ -1430,6 +1519,10 @@
330+
331+
this.func_70107_b((double)((float)blockpos.func_177958_n() + 0.5F), (double)((float)blockpos.func_177956_o() + 0.1F), (double)((float)blockpos.func_177952_p() + 0.5F));
332+
}
333+
+ else
334+
+ {
335+
+ p_70999_3_ = false;
336+
+ }
337+
338+
this.field_71083_bS = false;
339+
340+
@@ -1448,14 +1541,14 @@
327341

328342
private boolean func_175143_p()
329343
{
330344
- return this.field_70170_p.func_180495_p(this.field_71081_bT).func_177230_c() == Blocks.field_150324_C;
331-
+ return this.field_70170_p.func_180495_p(this.field_71081_bT).func_177230_c().isBed(field_70170_p, field_71081_bT, this);
345+
+ return net.minecraftforge.event.ForgeEventFactory.fireSleepingLocationCheck(this, field_71081_bT);
332346
}
333347

334348
public static BlockPos func_180467_a(World p_180467_0_, BlockPos p_180467_1_, boolean p_180467_2_)
@@ -340,7 +354,7 @@
340354
{
341355
if (!p_180467_2_)
342356
{
343-
@@ -1470,7 +1559,7 @@
357+
@@ -1470,16 +1563,16 @@
344358
}
345359
else
346360
{
@@ -349,16 +363,18 @@
349363
}
350364
}
351365

352-
@@ -1479,7 +1568,7 @@
366+
@SideOnly(Side.CLIENT)
367+
public float func_71051_bG()
353368
{
354-
if (this.field_71081_bT != null)
369+
- if (this.field_71081_bT != null)
370+
+ if (this.field_71081_bT != null && field_70170_p.func_180495_p(field_71081_bT).func_177230_c().isBed(field_70170_p, field_71081_bT, this))
355371
{
356372
- EnumFacing enumfacing = (EnumFacing)this.field_70170_p.func_180495_p(this.field_71081_bT).func_177229_b(BlockDirectional.field_176387_N);
357373
+ EnumFacing enumfacing = this.field_70170_p.func_180495_p(this.field_71081_bT).func_177230_c().getBedDirection(field_70170_p, field_71081_bT);
358374

359375
switch (enumfacing)
360376
{
361-
@@ -1519,16 +1608,24 @@
377+
@@ -1519,16 +1612,24 @@
362378

363379
public BlockPos func_180470_cg()
364380
{
@@ -385,7 +401,7 @@
385401
if (p_180473_1_ != null)
386402
{
387403
this.field_71077_c = p_180473_1_;
388-
@@ -1712,6 +1809,10 @@
404+
@@ -1712,6 +1813,10 @@
389405

390406
super.func_180430_e(p_180430_1_, p_180430_2_);
391407
}
@@ -396,15 +412,15 @@
396412
}
397413

398414
protected void func_71061_d_()
399-
@@ -1735,6 +1836,7 @@
415+
@@ -1735,6 +1840,7 @@
400416
}
401417

402418
EntityList.EntityEggInfo entitylist$entityegginfo = (EntityList.EntityEggInfo)EntityList.field_75627_a.get(Integer.valueOf(EntityList.func_75619_a(p_70074_1_)));
403419
+ if (entitylist$entityegginfo == null) entitylist$entityegginfo = net.minecraftforge.fml.common.registry.EntityRegistry.getEggs().get(EntityList.func_75621_b(p_70074_1_));
404420

405421
if (entitylist$entityegginfo != null)
406422
{
407-
@@ -1847,6 +1949,8 @@
423+
@@ -1847,6 +1953,8 @@
408424
{
409425
if (p_71008_1_ != this.field_71074_e)
410426
{
@@ -413,7 +429,7 @@
413429
this.field_71074_e = p_71008_1_;
414430
this.field_71072_f = p_71008_2_;
415431

416-
@@ -1918,6 +2022,10 @@
432+
@@ -1918,6 +2026,10 @@
417433
this.field_181016_an = p_71049_1_.field_181016_an;
418434
this.field_181017_ao = p_71049_1_.field_181017_ao;
419435
this.field_181018_ap = p_71049_1_.field_181018_ap;
@@ -424,7 +440,7 @@
424440
}
425441
else if (this.field_70170_p.func_82736_K().func_82766_b("keepInventory"))
426442
{
427-
@@ -1931,6 +2039,18 @@
443+
@@ -1931,6 +2043,18 @@
428444
this.field_175152_f = p_71049_1_.field_175152_f;
429445
this.field_71078_a = p_71049_1_.field_71078_a;
430446
this.func_70096_w().func_75692_b(10, Byte.valueOf(p_71049_1_.func_70096_w().func_75683_a(10)));
@@ -443,7 +459,7 @@
443459
}
444460

445461
protected boolean func_70041_e_()
446-
@@ -1968,7 +2088,14 @@
462+
@@ -1968,7 +2092,14 @@
447463

448464
public void func_70062_b(int p_70062_1_, ItemStack p_70062_2_)
449465
{
@@ -459,7 +475,7 @@
459475
}
460476

461477
@SideOnly(Side.CLIENT)
462-
@@ -2013,7 +2140,10 @@
478+
@@ -2013,7 +2144,10 @@
463479

464480
public IChatComponent func_145748_c_()
465481
{
@@ -471,7 +487,7 @@
471487
ichatcomponent.func_150256_b().func_150241_a(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/msg " + this.func_70005_c_() + " "));
472488
ichatcomponent.func_150256_b().func_150209_a(this.func_174823_aP());
473489
ichatcomponent.func_150256_b().func_179989_a(this.func_70005_c_());
474-
@@ -2022,7 +2152,7 @@
490+
@@ -2022,7 +2156,7 @@
475491

476492
public float func_70047_e()
477493
{
@@ -480,7 +496,7 @@
480496

481497
if (this.func_70608_bn())
482498
{
483-
@@ -2155,6 +2285,136 @@
499+
@@ -2155,6 +2289,136 @@
484500
this.field_175153_bG = p_175150_1_;
485501
}
486502

src/main/java/net/minecraftforge/event/ForgeEventFactory.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent;
6666
import net.minecraftforge.event.entity.player.PlayerUseItemEvent;
6767
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;
68+
import net.minecraftforge.event.entity.player.SleepingLocationCheckEvent;
6869
import net.minecraftforge.event.entity.player.UseHoeEvent;
6970
import net.minecraftforge.event.world.BlockEvent;
7071
import net.minecraftforge.event.world.BlockEvent.NeighborNotifyEvent;
@@ -500,4 +501,17 @@ private static CapabilityDispatcher gatherCapabilities(AttachCapabilitiesEvent e
500501
MinecraftForge.EVENT_BUS.post(event);
501502
return event.getCapabilities().size() > 0 ? new CapabilityDispatcher(event.getCapabilities(), parent) : null;
502503
}
504+
505+
public static boolean fireSleepingLocationCheck(EntityPlayer player, BlockPos sleepingLocation)
506+
{
507+
SleepingLocationCheckEvent evt = new SleepingLocationCheckEvent(player, sleepingLocation);
508+
MinecraftForge.EVENT_BUS.post(evt);
509+
510+
Result canContinueSleep = evt.getResult();
511+
if (canContinueSleep == Result.DEFAULT)
512+
return player.worldObj.getBlockState(player.playerLocation).getBlock().isBed(player.worldObj, player.playerLocation, player);
513+
else
514+
return canContinueSleep == Result.ALLOW;
515+
}
516+
503517
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package net.minecraftforge.event.entity.player;
2+
3+
import net.minecraft.entity.player.EntityPlayer;
4+
import net.minecraft.util.BlockPos;
5+
import net.minecraftforge.fml.common.eventhandler.Event.HasResult;
6+
7+
/**
8+
* This event is fired when game checks, if sleeping player should be still considered "in bed".<br>
9+
* Failing this check will cause player to wake up.<br>
10+
*
11+
* This event has a result. {@link HasResult}<br>
12+
*
13+
* setResult(ALLOW) informs game that player is still "in bed"<br>
14+
* setResult(DEFAULT) causes game to check {@link net.minecraft.block.Block#isBed(net.minecraft.world.IBlockAccess, BlockPos, net.minecraft.entity.Entity)} instead
15+
*/
16+
@HasResult
17+
public class SleepingLocationCheckEvent extends PlayerEvent
18+
{
19+
20+
public final BlockPos sleepingLocation;
21+
22+
public SleepingLocationCheckEvent(EntityPlayer player, BlockPos sleepingLocation)
23+
{
24+
super(player);
25+
this.sleepingLocation = sleepingLocation;
26+
}
27+
28+
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package net.minecraftforge.test;
2+
3+
import net.minecraft.creativetab.CreativeTabs;
4+
import net.minecraft.entity.Entity;
5+
import net.minecraft.entity.player.EntityPlayer;
6+
import net.minecraft.entity.player.EntityPlayer.EnumStatus;
7+
import net.minecraft.item.Item;
8+
import net.minecraft.item.ItemStack;
9+
import net.minecraft.nbt.NBTTagCompound;
10+
import net.minecraft.world.World;
11+
import net.minecraftforge.common.IExtendedEntityProperties;
12+
import net.minecraftforge.common.MinecraftForge;
13+
import net.minecraftforge.event.entity.EntityEvent.EntityConstructing;
14+
import net.minecraftforge.event.entity.player.PlayerWakeUpEvent;
15+
import net.minecraftforge.event.entity.player.SleepingLocationCheckEvent;
16+
import net.minecraftforge.fml.common.Mod;
17+
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
18+
import net.minecraftforge.fml.common.eventhandler.Event.Result;
19+
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
20+
import net.minecraftforge.fml.common.registry.GameRegistry;
21+
22+
@Mod(modid = NoBedSleepingTest.MODID, version = NoBedSleepingTest.VERSION)
23+
public class NoBedSleepingTest
24+
{
25+
public static final String MODID = "ForgeDebugNoBedSleeping";
26+
public static final String VERSION = "1.0";
27+
28+
@Mod.EventHandler
29+
public void preInit(FMLPreInitializationEvent event)
30+
{
31+
GameRegistry.registerItem(ItemSleepingPill.instance, ItemSleepingPill.name);
32+
MinecraftForge.EVENT_BUS.register(new EventHandler());
33+
}
34+
35+
public static class EventHandler
36+
{
37+
@SubscribeEvent
38+
public void onEntityConstruct(EntityConstructing evt)
39+
{
40+
evt.entity.registerExtendedProperties(ExtendedPropertySleeping.name, new ExtendedPropertySleeping());
41+
}
42+
43+
@SubscribeEvent
44+
public void onBedCheck(SleepingLocationCheckEvent evt)
45+
{
46+
final IExtendedEntityProperties property = evt.entityPlayer.getExtendedProperties(ExtendedPropertySleeping.name);
47+
if (property instanceof ExtendedPropertySleeping && ((ExtendedPropertySleeping) property).isSleeping)
48+
evt.setResult(Result.ALLOW);
49+
}
50+
51+
@SubscribeEvent
52+
public void onWakeUp(PlayerWakeUpEvent evt)
53+
{
54+
final IExtendedEntityProperties property = evt.entityPlayer.getExtendedProperties(ExtendedPropertySleeping.name);
55+
if (property instanceof ExtendedPropertySleeping)
56+
((ExtendedPropertySleeping) property).isSleeping = false;
57+
}
58+
}
59+
60+
public static class ExtendedPropertySleeping implements IExtendedEntityProperties
61+
{
62+
private static final String TAG_NAME = "IsSleepingExt";
63+
64+
public static final String name = "is_sleeping";
65+
66+
public boolean isSleeping;
67+
68+
@Override
69+
public void saveNBTData(NBTTagCompound compound)
70+
{
71+
compound.setBoolean(TAG_NAME, isSleeping);
72+
}
73+
74+
@Override
75+
public void loadNBTData(NBTTagCompound compound)
76+
{
77+
this.isSleeping = compound.getBoolean(TAG_NAME);
78+
}
79+
80+
@Override
81+
public void init(Entity entity, World world)
82+
{
83+
}
84+
85+
}
86+
87+
public static class ItemSleepingPill extends Item
88+
{
89+
public static final ItemSleepingPill instance = new ItemSleepingPill();
90+
public static final String name = "sleeping_pill";
91+
92+
private ItemSleepingPill()
93+
{
94+
setCreativeTab(CreativeTabs.tabMisc);
95+
setUnlocalizedName(MODID + ":" + name);
96+
}
97+
98+
public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityPlayer playerIn)
99+
{
100+
if (!worldIn.isRemote)
101+
{
102+
final EnumStatus result = playerIn.trySleep(playerIn.getPosition());
103+
if (result == EnumStatus.OK)
104+
{
105+
final IExtendedEntityProperties property = playerIn.getExtendedProperties(ExtendedPropertySleeping.name);
106+
if (property instanceof ExtendedPropertySleeping)
107+
((ExtendedPropertySleeping) property).isSleeping = true;
108+
}
109+
}
110+
return itemStackIn;
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)