@Override public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) { ItemStack stack = player.getHeldItem(hand); if (!world.isRemote) { EntityPlayerMP mp = (EntityPlayerMP) player; // final EntityPlayer.SleepResult result = player.trySleep(player.getPosition()); //trySleep was changed in 1.11.2 to literally check for the specific exact Blocks.BED in world. because fuck modders amirite? //and it just assumes unsafely its there and then dies. EntityPlayer.SleepResult result = this.canPlayerSleep(player, world); if (result == EntityPlayer.SleepResult.OK) { final IPlayerExtendedProperties sleep = CapabilityRegistry.getPlayerProperties(player); if (sleep != null) { onSleepSuccess(world, hand, stack, mp, sleep); } else { //should never happen... but just in case UtilChat.addChatMessage(player, "tile.bed.noSleep"); } } else { UtilChat.addChatMessage(player, "tile.bed.noSleep"); } } return ActionResult.newResult(EnumActionResult.SUCCESS, stack); }
private static boolean trySleep(World world, EntityPlayer player) { final BlockPos pos = player.getPosition(); if (!isNotSuffocating(world, pos) || !isSolidEnough(world, pos.down())) { player.sendMessage(new TextComponentTranslation("openblocks.misc.oh_no_ground")); return false; } final EntityPlayer.SleepResult sleepResult = player.trySleep(pos); if (sleepResult == SleepResult.OK) return true; switch (sleepResult) { case NOT_POSSIBLE_NOW: player.sendMessage(new TextComponentTranslation("tile.bed.noSleep")); break; case NOT_SAFE: player.sendMessage(new TextComponentTranslation("tile.bed.notSafe")); break; default: break; } return false; }
/** * hack in the vanilla sleep test, or at least something similar * * @param player * @param world * @return */ private SleepResult canPlayerSleep(EntityPlayer player, World world) { if (player.isEntityAlive() == false) { return EntityPlayer.SleepResult.OTHER_PROBLEM; } if (world.isDaytime()) { return EntityPlayer.SleepResult.NOT_POSSIBLE_NOW; } PlayerSleepInBedEvent event = new PlayerSleepInBedEvent(player, player.getPosition()); MinecraftForge.EVENT_BUS.post(event); if (event.getResultStatus() != null) { return event.getResultStatus(); } return EntityPlayer.SleepResult.OK; }
@SubscribeEvent public void handleSleepInBed(PlayerSleepInBedEvent event) { EntityPlayer p = event.getEntityPlayer(); final IPlayerExtendedProperties sleep = p.getCapability(ModCyclic.CAPABILITYSTORAGE, null); if (sleep != null && sleep.isSleeping()) { event.setResult(EntityPlayer.SleepResult.OK); } }
public SleepResult getResultStatus() { return result; }
public void setResult(SleepResult result) { this.result = result; }
public static SleepResult onPlayerSleepInBed(EntityPlayer player, BlockPos pos) { PlayerSleepInBedEvent event = new PlayerSleepInBedEvent(player, pos); MinecraftForge.EVENT_BUS.post(event); return event.getResultStatus(); }
public static SleepResult addStatus(String name) { return addEnum(SleepResult.class, name); }