-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileBuffer.java
104 lines (79 loc) · 2.32 KB
/
TileBuffer.java
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
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.core.lib;
import net.minecraft.block.Block;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import buildcraft.api.core.SafeTimeTracker;
import buildcraft.core.lib.utils.BlockUtils;
public final class TileBuffer {
private Block block = null;
private TileEntity tile;
private final SafeTimeTracker tracker = new SafeTimeTracker(20, 5);
private final World world;
private final int x, y, z;
private final boolean loadUnloaded;
public TileBuffer(World world, int x, int y, int z, boolean loadUnloaded) {
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.loadUnloaded = loadUnloaded;
refresh();
}
public void refresh() {
tile = null;
block = null;
if (!loadUnloaded && !world.blockExists(x, y, z)) {
return;
}
block = BlockUtils.getBlock(world, x, y, z);
if (block != null && block.hasTileEntity(BlockUtils.getBlockMetadata(world, x, y, z))) {
tile = BlockUtils.getTileEntity(world, x, y, z);
}
}
public void set(Block block, TileEntity tile) {
this.block = block;
this.tile = tile;
tracker.markTime(world);
}
public Block getBlock() {
if ((tile != null && tile.isInvalid()) || (tile == null && tracker.markTimeIfDelay(world))) {
refresh();
}
return block;
}
public TileEntity getTile() {
if (tile != null && !tile.isInvalid()) {
return tile;
}
if (tracker.markTimeIfDelay(world)) {
refresh();
if (tile != null && !tile.isInvalid()) {
return tile;
}
}
return null;
}
public boolean exists() {
if (tile != null && !tile.isInvalid()) {
return true;
}
return world.blockExists(x, y, z);
}
public static TileBuffer[] makeBuffer(World world, int x, int y, int z, boolean loadUnloaded) {
TileBuffer[] buffer = new TileBuffer[6];
for (int i = 0; i < 6; i++) {
ForgeDirection d = ForgeDirection.getOrientation(i);
buffer[i] = new TileBuffer(world, x + d.offsetX, y + d.offsetY, z + d.offsetZ, loadUnloaded);
}
return buffer;
}
}