This repository was archived by the owner on Dec 2, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTileEntityComputerCore.java
More file actions
285 lines (246 loc) · 6.78 KB
/
TileEntityComputerCore.java
File metadata and controls
285 lines (246 loc) · 6.78 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package teamcerberus.cerberustech.computer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet132TileEntityData;
import net.minecraft.tileentity.TileEntity;
public class TileEntityComputerCore extends TileEntity implements IInventory,
IComputerTE {
public int[][] clientPixels;
private Computer computer;
private int id;
private Thread thread;
public boolean running;
public TileEntityComputerCore() {
clientPixels = new int[200][200];
running = false;
id = -1;
}
@Override
public void sendPacket() {
notifyNeighbors();
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
}
@Override
public void startComputer() {
if (running) {
return;
} else {
if (id == -1) {
id = IDGenerator.getNextID("CTComputer");
}
notifyNeighbors();
running = true;
computer = new Computer(id, this);
thread = new Thread(computer);
thread.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread arg0, Throwable arg1) {
}
});
thread.start();
}
}
@SuppressWarnings("deprecation")
@Override
public void stopComputer() {
if (!running) {
return;
} else {
try {
running = false;
notifyNeighbors();
thread.interrupt();
thread.stop();
} catch (Exception e) {
}
sendPacket();
}
}
public void blockDestroy() {
stopComputer();
}
public void keyboardEvent(OSKeyboardEvents eventFromID,
OSKeyboardLetters fromID) {
if(running)
computer.keyboardEvent(eventFromID, fromID);
}
@Override
public void readFromNBT(NBTTagCompound par1nbtTagCompound) {
super.readFromNBT(par1nbtTagCompound);
id = par1nbtTagCompound.getInteger("computer-id");
}
@Override
public void writeToNBT(NBTTagCompound par1nbtTagCompound) {
super.writeToNBT(par1nbtTagCompound);
par1nbtTagCompound.setInteger("computer-id", id);
}
@Override
public Packet getDescriptionPacket() {
NBTTagCompound com = new NBTTagCompound();
com.setByteArray(
"pixels",
convertToByteArray(convertToOneDim(running ? computer
.getMonitorPixels() : new int[200][200])));
return new Packet132TileEntityData(xCoord, yCoord, zCoord, 0, com);
}
@Override
public void onDataPacket(INetworkManager net, Packet132TileEntityData pkt) {
NBTTagCompound com = pkt.customParam1;
clientPixels = convertFromOneDim(
convertFromByteArray(com.getByteArray("pixels")), 200, 200);
}
private static int[] convertToOneDim(int ints[][]) {
ArrayList<Integer> list = new ArrayList<Integer>();
for (int[] k : ints) {
for (int j = 0; j < k.length; j++) {
list.add(k[j]);
}
}
int result[] = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
private static int[][] convertFromOneDim(int ints[], int numberOfRows,
int rowSize) {
int returnArray[][] = new int[numberOfRows][rowSize];
for (int i = 0; i < numberOfRows; i++) {
for (int j = 0; j < rowSize; j++) {
returnArray[i][j] = ints[i * rowSize + j];
}
}
return returnArray;
}
private static byte[] convertToByteArray(int[] values) {
ByteBuffer byteBuffer = ByteBuffer.allocate(values.length * 4);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(values);
return byteBuffer.array();
}
private static int[] convertFromByteArray(byte[] bytes) {
ByteBuffer buff = ByteBuffer.allocate(bytes.length);
buff.put(bytes);
buff.rewind();
IntBuffer intbuff = buff.asIntBuffer();
int[] dest = new int[bytes.length / 4];
intbuff.get(dest);
return dest;
}
/* ----> INV Stuff <---- */
private ItemStack ComputerItemStacks[];
@Override
public int getSizeInventory() {
return ComputerItemStacks.length;
}
@Override
public ItemStack getStackInSlot(int i) {
return ComputerItemStacks[i];
}
@Override
public ItemStack decrStackSize(int i, int j) {
if (ComputerItemStacks[i] != null) {
if (ComputerItemStacks[i].stackSize <= j) {
ItemStack itemstack = ComputerItemStacks[i];
ComputerItemStacks[i] = null;
return itemstack;
}
ItemStack itemstack1 = ComputerItemStacks[i].splitStack(j);
if (ComputerItemStacks[i].stackSize == 0) {
ComputerItemStacks[i] = null;
}
return itemstack1;
} else {
return null;
}
}
@Override
public void setInventorySlotContents(int i, ItemStack itemstack) {
ComputerItemStacks[i] = itemstack;
if (itemstack != null && itemstack.stackSize > getInventoryStackLimit()) {
itemstack.stackSize = getInventoryStackLimit();
}
}
@Override
public String getInvName() {
return "Computer";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
@Override
public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) {
return worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this ? false
: par1EntityPlayer.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D,
zCoord + 0.5D) <= 64.0D;
}
public boolean canInteractWith(EntityPlayer entityplayer) {
if (worldObj.getBlockTileEntity(xCoord, yCoord, zCoord) != this) {
return false;
}
return entityplayer.getDistanceSq(xCoord + 0.5D, yCoord + 0.5D,
zCoord + 0.5D) <= 64D;
}
@Override
public void openChest() {
}
@Override
public void closeChest() {
}
@Override
public ItemStack getStackInSlotOnClosing(int par1) {
if (ComputerItemStacks[par1] != null) {
ItemStack var2 = ComputerItemStacks[par1];
ComputerItemStacks[par1] = null;
return var2;
} else {
return null;
}
}
@Override
public boolean isInvNameLocalized() {
return true;
}
@Override
public boolean isItemValidForSlot(int i, ItemStack itemstack) {
return true;
}
public int getRedstoneOutput(LocalDirection side) {
if (running)
return computer.getRedstoneOutput(side);
return 0;
}
@Override
public void notifyNeighbors() {
worldObj.notifyBlocksOfNeighborChange(xCoord, yCoord, zCoord,
worldObj.getBlockId(xCoord, yCoord, zCoord));
}
public int isBlockProvidingPowerOnSide(int i1, int j1, int k1, int l) {
int i = xCoord + i1;
int j = yCoord + j1;
int k = zCoord + k1;
if ((j >= 0) && (j < worldObj.getHeight())) {
return Math
.max(Math.max(
worldObj.getBlockId(i, j, k) == Block.redstoneWire.blockID ? worldObj
.getBlockMetadata(i, j, k) : 0, worldObj
.getIndirectPowerLevelTo(i, j, k, l)),
worldObj.isBlockProvidingPowerTo(i, j, k, l));
}
return 0;
}
@Override
public int getSideFacing() {
return worldObj.getBlockMetadata(xCoord, yCoord, zCoord);
}
}