Jump to content

[UNSOLVED] Cant sleep in custom bed


Manslaughter777

Recommended Posts

I click on the bed and i sleep for about half a second, then get back up and its still night. Copied from BlockBed, am is missing something?

 

package com.manslaughter777.crystaldimension.block;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

import java.util.Iterator;
import java.util.Random;

import com.manslaughter777.crystaldimension.Main;

import net.minecraft.block.Block;
import net.minecraft.block.BlockBed;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.util.Direction;
import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeGenBase;

public class BlockMattress extends BlockDirectional
{
    public static final int[][] field_149981_a = new int[][] {{0, 1}, { -1, 0}, {0, -1}, {1, 0}};
    @SideOnly(Side.CLIENT)
    private IIcon[] endIcon;
    @SideOnly(Side.CLIENT)
    private IIcon[] sideIcon;
    @SideOnly(Side.CLIENT)
    private IIcon[] topIcon;

    public BlockMattress()
    {
        super(Material.cloth);
        this.func_149978_e();
    }

    /**
     * Called upon block activation (right click on the block.)
     */
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer player, int p6, float p7, float p8, float p9)
    {
        if (world.isRemote)
        {
            return true;
        }
        else
        {
            int i1 = world.getBlockMetadata(x, y, z);

            if (!isBlockHeadOfBed(i1))
            {
                int j1 = getDirection(i1);
                x += field_149981_a[j1][0];
                z += field_149981_a[j1][1];

                if (world.getBlock(x, y, z) != this)
                {
                    return true;
                }

                i1 = world.getBlockMetadata(x, y, z);
            }

            if (world.provider.canRespawnHere() && world.getBiomeGenForCoords(x, z) == Main.crystalBiome)
            {
                if (func_149976_c(i1))
                {
                    EntityPlayer entityplayer1 = null;
                    Iterator iterator = world.playerEntities.iterator();

                    while (iterator.hasNext())
                    {
                        EntityPlayer entityplayer2 = (EntityPlayer)iterator.next();

                        if (entityplayer2.isPlayerSleeping())
                        {
                            ChunkCoordinates chunkcoordinates = entityplayer2.playerLocation;

                            if (chunkcoordinates.posX == x && chunkcoordinates.posY == y && chunkcoordinates.posZ == z)
                            {
                                entityplayer1 = entityplayer2;
                            }
                        }
                    }

                    if (entityplayer1 != null)
                    {
                        player.addChatComponentMessage(new ChatComponentTranslation("tile.bed.occupied", new Object[0]));
                        return true;
                    }

                    func_149979_a(world, x, y, z, false);
                }

                EntityPlayer.EnumStatus enumstatus = player.sleepInBedAt(x, y, z);

                if (enumstatus == EntityPlayer.EnumStatus.OK)
                {
                    func_149979_a(world, x, y, z, true);
                    return true;
                }
                else
                {
                    if (enumstatus == EntityPlayer.EnumStatus.NOT_POSSIBLE_NOW)
                    {
                        player.addChatComponentMessage(new ChatComponentTranslation("tile.bed.noSleep", new Object[0]));
                    }
                    else if (enumstatus == EntityPlayer.EnumStatus.NOT_SAFE)
                    {
                        player.addChatComponentMessage(new ChatComponentTranslation("tile.bed.notSafe", new Object[0]));
                    }

                    return true;
                }
            }
            else
            {
                double d2 = (double)x + 0.5D;
                double d0 = (double)y + 0.5D;
                double d1 = (double)z + 0.5D;
                world.setBlockToAir(x, y, z);
                int k1 = getDirection(i1);
                x += field_149981_a[k1][0];
                z += field_149981_a[k1][1];

                if (world.getBlock(x, y, z) == this)
                {
                    world.setBlockToAir(x, y, z);
                    d2 = (d2 + (double)x + 0.5D) / 2.0D;
                    d0 = (d0 + (double)y + 0.5D) / 2.0D;
                    d1 = (d1 + (double)z + 0.5D) / 2.0D;
                }

                player.addChatMessage(new ChatComponentTranslation("tile.bed.noUse"));
                return true;
            }
        }
    }

    /**
     * Gets the block's texture. Args: side, meta
     */
    @SideOnly(Side.CLIENT)
    public IIcon getIcon(int side, int meta)
    {
        if (side == 0)
        {
            return Blocks.planks.getBlockTextureFromSide(side);
        }
        else
        {
            int k = getDirection(meta);
            int l = Direction.bedDirection[k][side];
            int i1 = isBlockHeadOfBed(meta) ? 1 : 0;
            return (i1 != 1 || l != 2) && (i1 != 0 || l != 3) ? (l != 5 && l != 4 ? this.topIcon[i1] : this.sideIcon[i1]) : this.endIcon[i1];
        }
    }

    @SideOnly(Side.CLIENT)
    public void registerBlockIcons(IIconRegister iconRegistry)
    {
        this.topIcon = new IIcon[] {iconRegistry.registerIcon(this.getTextureName() + "_feet_top"), iconRegistry.registerIcon(this.getTextureName() + "_head_top")};
        this.endIcon = new IIcon[] {iconRegistry.registerIcon(this.getTextureName() + "_feet_end"), iconRegistry.registerIcon(this.getTextureName() + "_head_end")};
        this.sideIcon = new IIcon[] {iconRegistry.registerIcon(this.getTextureName() + "_feet_side"), iconRegistry.registerIcon(this.getTextureName() + "_head_side")};
    }

    /**
     * The type of render function that is called for this block
     */
    public int getRenderType()
    {
        return 14;
    }

    /**
     * If this block doesn't render as an ordinary block it will return False (examples: signs, buttons, stairs, etc)
     */
    public boolean renderAsNormalBlock()
    {
        return false;
    }

    /**
     * Is this block (a) opaque and (b) a full 1m cube?  This determines whether or not to render the shared face of two
     * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block.
     */
    public boolean isOpaqueCube()
    {
        return false;
    }

    /**
     * Updates the blocks bounds based on its current state. Args: world, x, y, z
     */
    public void setBlockBoundsBasedOnState(IBlockAccess worldBlock, int x, int y, int z)
    {
        this.func_149978_e();
    }

    /**
     * Lets the block know when one of its neighbor changes. Doesn't know which neighbor changed (coordinates passed are
     * their own) Args: x, y, z, neighbor Block
     */
    public void onNeighborBlockChange(World world, int x, int y, int z, Block block)
    {
        int l = world.getBlockMetadata(x, y, z);
        int i1 = getDirection(l);

        if (isBlockHeadOfBed(l))
        {
            if (world.getBlock(x - field_149981_a[i1][0], y, z - field_149981_a[i1][1]) != this)
            {
                world.setBlockToAir(x, y, z);
            }
        }
        else if (world.getBlock(x + field_149981_a[i1][0], y, z + field_149981_a[i1][1]) != this)
        {
            world.setBlockToAir(x, y, z);

            if (!world.isRemote)
            {
                this.dropBlockAsItem(world, x, y, z, l, 0);
            }
        }
    }

    public Item getItemDropped(int direction, Random random, int unused)
    {
        /**
         * Returns whether or not this bed block is the head of the bed.
         */
        return isBlockHeadOfBed(direction) ? Item.getItemById(0) : Main.mattress;
    }

    private void func_149978_e()
    {
        this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5625F, 1.0F);
    }

    /**
     * Returns whether or not this bed block is the head of the bed.
     */
    public static boolean isBlockHeadOfBed(int direction)
    {
        return (direction &  != 0;
    }

    public static boolean func_149976_c(int direction)
    {
        return (direction & 4) != 0;
    }

    public static void func_149979_a(World world, int x, int y, int z, boolean isOccupied)
    {
        int l = world.getBlockMetadata(x, y, z);

        if (isOccupied)
        {
            l |= 4;
        }
        else
        {
            l &= -5;
        }

        world.setBlockMetadataWithNotify(x, y, z, l, 4);
    }

    public static ChunkCoordinates func_149977_a(World world, int x, int y, int z, int p5)
    {
        int i1 = world.getBlockMetadata(x, y, z);
        int j1 = BlockDirectional.getDirection(i1);

        for (int k1 = 0; k1 <= 1; ++k1)
        {
            int l1 = x - field_149981_a[j1][0] * k1 - 1;
            int i2 = z - field_149981_a[j1][1] * k1 - 1;
            int j2 = l1 + 2;
            int k2 = i2 + 2;

            for (int l2 = l1; l2 <= j2; ++l2)
            {
                for (int i3 = i2; i3 <= k2; ++i3)
                {
                    if (World.doesBlockHaveSolidTopSurface(world, l2, y - 1, i3) && !world.getBlock(l2, y, i3).getMaterial().isOpaque() && !world.getBlock(l2, y + 1, i3).getMaterial().isOpaque())
                    {
                        if (p5 <= 0)
                        {
                            return new ChunkCoordinates(l2, y, i3);
                        }

                        --p5;
                    }
                }
            }
        }

        return null;
    }

    /**
     * Drops the block items with a specified chance of dropping the specified items
     */
    public void dropBlockAsItemWithChance(World world, int x, int y, int z, int direction, float p6, int p7)
    {
        if (!isBlockHeadOfBed(direction))
        {
            super.dropBlockAsItemWithChance(world, x, y, z, direction, p6, 0);
        }
    }

    /**
     * Returns the mobility information of the block, 0 = free, 1 = can't push but can move over, 2 = total immobility
     * and stop pistons
     */
    public int getMobilityFlag()
    {
        return 1;
    }

    /**
     * Gets an item for the block being called on. Args: world, x, y, z
     */
    @SideOnly(Side.CLIENT)
    public Item getItem(World world, int x, int y, int z)
    {
        return Main.mattress;
    }

    /**
     * Called when the block is attempted to be harvested
     */
    public void onBlockHarvested(World world, int x, int y, int z, int direction, EntityPlayer player)
    {
        if (player.capabilities.isCreativeMode && isBlockHeadOfBed(direction))
        {
            int i1 = getDirection(direction);
            x -= field_149981_a[i1][0];
            z -= field_149981_a[i1][1];

            if (world.getBlock(x, y, z) == this)
            {
                world.setBlockToAir(x, y, z);
            }
        }
    }
}

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Please share a link to your crash report on https://paste.ee, as explained in the FAQ. Dumping it directly into the thread often triggers the anti-spam and makes it hard to read due to word wrapping
    • Hi all, I'm running a 1.20.1 Minecraft Forge server with a hosting website and something seems to have gone wrong. It started as an occasional crash that has now become extremely frequent and I don't know what exactly is causing it. I already tried disabling watchdog but the server crashes anyway. Oddly it only occasionally generates a crash report after a crash occurs. Based on viewing the server console's research usage it seems that the CPU spikes astronomically high (100% usage), especially when chunks are being loaded or when a new person joins the server. I downloaded Spark to create a profile (https://spark.lucko.me/bBlWe7ou29) but I lack experience with this mod and don't understand what I'm looking at/how this should help me diagnose the issue. I am using a lot of mods so I'm guessing there is some sort of issue there or a corrupted chunk (although MCRF found no chunk errors). We actually don't experience any lag until the crash occurs, so it's some sort of sudden spike in resource usage. Based on the crash report I've already tried removing Canary and Modernfix but there was no change. Thanks for any help you can provide. Specs:  4 cores x 4200MHz AMD Ryzen 9 5950X 4GB RAM Crash report:  
    • will test that once i get home, thanks
    • I am getting this exception (I am new and just trying to create an item) Caused by: java.io.FileNotFoundException: abrasmagics:models/item/ingot_test.json at net.minecraft.client.resources.SimpleReloadableResourceManager.getResource(SimpleReloadableResourceManager.java:69) ~[SimpleReloadableResourceManager.class:?] at net.minecraft.client.renderer.block.model.ModelBakery.loadModel(ModelBakery.java:334) ~[ModelBakery.class:?] at net.minecraftforge.client.model.ModelLoader.access$1400(ModelLoader.java:115) ~[ModelLoader.class:?] at net.minecraftforge.client.model.ModelLoader$VanillaLoader.loadModel(ModelLoader.java:861) ~[ModelLoader$VanillaLoader.class:?] at net.minecraftforge.client.model.ModelLoaderRegistry.getModel(ModelLoaderRegistry.java:157) ~[ModelLoaderRegistry.class:?]   However the directory is set up correctly and the file does exist   idk how to upload images so here is a treeview of the directories:   src ├───main │ ├───java │ │ └───net │ │ └───abrasmods │ │ └───abrasmagics │ │ ├───init │ │ ├───items │ │ └───utils │ │ ├───handlers │ │ ├───misc │ │ └───proxy │ └───resources │ └───assets │ └───abrasmagics │ ├───models │ │ ├───block │ │ └───item │ └───textures └───test ├───java └───resources   the file ingot_test.json is under models/item. The directory seems fine and the file does exist so anyone plz help.   i am using intellij and is coding for 1.12.2
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.