/
TileEntityColoredRotatable.java
76 lines (63 loc) · 1.88 KB
/
TileEntityColoredRotatable.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
package choonster.testmod3.tileentity;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SPacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import javax.annotation.Nullable;
/**
* A TileEntity that stores an {@link EnumFacing} value.
*
* @author Choonster
*/
public class TileEntityColoredRotatable extends TileEntity {
private EnumFacing facing = EnumFacing.NORTH;
public EnumFacing getFacing() {
return facing;
}
public void setFacing(EnumFacing facing) {
this.facing = facing;
markDirty();
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
facing = EnumFacing.getFront(compound.getInteger("facing"));
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
compound.setInteger("facing", facing.getIndex());
return compound;
}
private void notifyBlockUpdate(){
final IBlockState state = getWorld().getBlockState(getPos());
getWorld().notifyBlockUpdate(getPos(), state, state, 3);
}
@Override
public void markDirty() {
super.markDirty();
notifyBlockUpdate();
}
@Override
public NBTTagCompound getUpdateTag() {
return writeToNBT(new NBTTagCompound());
}
@Nullable
@Override
public SPacketUpdateTileEntity getUpdatePacket() {
return new SPacketUpdateTileEntity(getPos(), 0, getUpdateTag());
}
@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
readFromNBT(pkt.getNbtCompound());
notifyBlockUpdate();
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
return oldState.getBlock() != newSate.getBlock();
}
}