-
Notifications
You must be signed in to change notification settings - Fork 497
/
TileBuildCraft.java
148 lines (123 loc) · 4.04 KB
/
TileBuildCraft.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
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
/**
* Copyright (c) SpaceToad, 2011 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;
import buildcraft.api.power.IPowerReceptor;
import buildcraft.core.network.ISynchronizedTile;
import buildcraft.core.network.PacketPayload;
import buildcraft.core.network.PacketPayloadArrays;
import buildcraft.core.network.PacketTileUpdate;
import buildcraft.core.network.PacketUpdate;
import buildcraft.core.network.TilePacketWrapper;
import buildcraft.core.proxy.CoreProxy;
import buildcraft.core.utils.Utils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.Packet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
public abstract class TileBuildCraft extends TileEntity implements ISynchronizedTile {
@SuppressWarnings("rawtypes")
private static Map<Class, TilePacketWrapper> updateWrappers = new HashMap<Class, TilePacketWrapper>();
@SuppressWarnings("rawtypes")
private static Map<Class, TilePacketWrapper> descriptionWrappers = new HashMap<Class, TilePacketWrapper>();
private final TilePacketWrapper descriptionPacket;
private final TilePacketWrapper updatePacket;
private boolean init = false;
private String owner = "[BuildCraft]";
public TileBuildCraft() {
if (!updateWrappers.containsKey(this.getClass())) {
updateWrappers.put(this.getClass(), new TilePacketWrapper(this.getClass()));
}
if (!descriptionWrappers.containsKey(this.getClass())) {
descriptionWrappers.put(this.getClass(), new TilePacketWrapper(this.getClass()));
}
updatePacket = updateWrappers.get(this.getClass());
descriptionPacket = descriptionWrappers.get(this.getClass());
}
public String getOwner() {
return owner;
}
@Override
public void updateEntity() {
if (!init && !isInvalid()) {
initialize();
init = true;
}
if (this instanceof IPowerReceptor) {
IPowerReceptor receptor = ((IPowerReceptor) this);
receptor.getPowerReceiver(null).update();
}
}
@Override
public void invalidate() {
init = false;
super.invalidate();
}
public void initialize() {
Utils.handleBufferedDescription(this);
}
public void onBlockPlacedBy(EntityLivingBase entity, ItemStack stack) {
if (entity instanceof EntityPlayer)
owner = ((EntityPlayer) entity).username;
}
public void destroy() {
}
public void sendNetworkUpdate() {
if (CoreProxy.proxy.isSimulating(worldObj)) {
CoreProxy.proxy.sendToPlayers(getUpdatePacket(), worldObj, xCoord, yCoord, zCoord, DefaultProps.NETWORK_UPDATE_RANGE);
}
}
@Override
public Packet getDescriptionPacket() {
return new PacketTileUpdate(this).getPacket();
}
@Override
public PacketPayload getPacketPayload() {
return updatePacket.toPayload(this);
}
@Override
public Packet getUpdatePacket() {
return new PacketTileUpdate(this).getPacket();
}
@Override
public void handleDescriptionPacket(PacketUpdate packet) throws IOException {
if (packet.payload instanceof PacketPayloadArrays)
descriptionPacket.fromPayload(this, (PacketPayloadArrays) packet.payload);
}
@Override
public void handleUpdatePacket(PacketUpdate packet) throws IOException {
if (packet.payload instanceof PacketPayloadArrays)
updatePacket.fromPayload(this, (PacketPayloadArrays) packet.payload);
}
@Override
public void postPacketHandling(PacketUpdate packet) {
}
@Override
public void writeToNBT(NBTTagCompound nbt) {
super.writeToNBT(nbt);
nbt.setString("owner", owner);
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
if (nbt.hasKey("owner"))
owner = nbt.getString("owner");
}
public boolean isInvNameLocalized() {
// TODO Auto-generated method stub
return false;
}
public World getWorld() {
return worldObj;
}
}