summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/minecraft/server/BlockBed.java
blob: 403fedfdccd9eade905cba3b25ddb2ee1d07928c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package net.minecraft.server;

import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.event.block.BlockInteractEvent;

import java.util.Random;

public class BlockBed extends Block {

    public static final int[][] a = new int[][] { { 0, 1}, { -1, 0}, { 0, -1}, { 1, 0}};

    public BlockBed(int i) {
        super(i, 134, Material.CLOTH);
        this.f();
    }

    public boolean a(World world, int i, int j, int k, EntityHuman entityhuman) {
        int l = world.getData(i, j, k);

        if (!d(l)) {
            int i1 = c(l);

            i += a[i1][0];
            k += a[i1][1];
            if (world.getTypeId(i, j, k) != this.id) {
                return true;
            }

            l = world.getData(i, j, k);
        }

        // CraftBukkit start - Interact Bed
        CraftWorld craftWorld = ((WorldServer) world).getWorld();
        CraftServer server = ((WorldServer) world).getServer();
        Event.Type eventType = Event.Type.BLOCK_INTERACT;
        CraftBlock block = (CraftBlock) craftWorld.getBlockAt(i, j, k);
        LivingEntity who = (entityhuman == null) ? null : (LivingEntity) entityhuman.getBukkitEntity();

        BlockInteractEvent event = new BlockInteractEvent(eventType, block, who);
        server.getPluginManager().callEvent(event);

        if (event.isCancelled()) {
            return true;
        }
        // CraftBukkit end

        if (f(l)) {
            entityhuman.a("tile.bed.occupied");
            return true;
        } else if (entityhuman.a(i, j, k)) {
            a(world, i, j, k, true);
            return true;
        } else {
            entityhuman.a("tile.bed.noSleep");
            return true;
        }
    }

    public int a(int i, int j) {
        if (i == 0) {
            return Block.WOOD.textureId;
        } else {
            int k = c(j);
            int l = BedBlockTextures.c[k][i];

            return d(j) ? (l == 2 ? this.textureId + 2 + 16 : (l != 5 && l != 4 ? this.textureId + 1 : this.textureId + 1 + 16)) : (l == 3 ? this.textureId - 1 + 16 : (l != 5 && l != 4 ? this.textureId : this.textureId + 16));
        }
    }

    public boolean a() {
        return false;
    }

    public void a(IBlockAccess iblockaccess, int i, int j, int k) {
        this.f();
    }

    public void a(World world, int i, int j, int k, int l) {
        int i1 = world.getData(i, j, k);
        int j1 = c(i1);

        if (d(i1)) {
            if (world.getTypeId(i - a[j1][0], j, k - a[j1][1]) != this.id) {
                world.e(i, j, k, 0);
            }
        } else if (world.getTypeId(i + a[j1][0], j, k + a[j1][1]) != this.id) {
            world.e(i, j, k, 0);
            if (!world.isStatic) {
                this.b_(world, i, j, k, i1);
            }
        }
    }

    public int a(int i, Random random) {
        return d(i) ? 0 : Item.BED.id;
    }

    private void f() {
        this.a(0.0F, 0.0F, 0.0F, 1.0F, 0.5625F, 1.0F);
    }

    public static int c(int i) {
        return i & 3;
    }

    public static boolean d(int i) {
        return (i & 8) != 0;
    }

    public static boolean f(int i) {
        return (i & 4) != 0;
    }

    public static void a(World world, int i, int j, int k, boolean flag) {
        int l = world.getData(i, j, k);

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

        world.c(i, j, k, l);
    }

    public static ChunkCoordinates g(World world, int i, int j, int k, int l) {
        int i1 = world.getData(i, j, k);
        int j1 = c(i1);

        for (int k1 = 0; k1 <= 1; ++k1) {
            int l1 = i - a[j1][0] * k1 - 1;
            int i2 = k - 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.d(l2, j - 1, i3) && world.isEmpty(l2, j, i3) && world.isEmpty(l2, j + 1, i3)) {
                        if (l <= 0) {
                            return new ChunkCoordinates(l2, j, i3);
                        }

                        --l;
                    }
                }
            }
        }

        return new ChunkCoordinates(i, j + 1, k);
    }
}