-
Notifications
You must be signed in to change notification settings - Fork 497
/
ContainerZonePlan.java
executable file
·143 lines (124 loc) · 4.67 KB
/
ContainerZonePlan.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
/**
* 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.robotics.gui;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.material.MapColor;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import cpw.mods.fml.relauncher.Side;
import buildcraft.BuildCraftCore;
import buildcraft.BuildCraftRobotics;
import buildcraft.core.ZonePlan;
import buildcraft.core.lib.gui.BuildCraftContainer;
import buildcraft.core.lib.gui.slots.SlotOutput;
import buildcraft.core.lib.network.command.CommandWriter;
import buildcraft.core.lib.network.command.ICommandReceiver;
import buildcraft.core.lib.network.command.PacketCommand;
import buildcraft.core.lib.render.DynamicTexturePaletted;
import buildcraft.core.lib.utils.NetworkUtils;
import buildcraft.robotics.TileZonePlan;
public class ContainerZonePlan extends BuildCraftContainer implements ICommandReceiver {
public DynamicTexturePaletted mapTexture;
public ZonePlan currentAreaSelection;
public GuiZonePlan gui;
private TileZonePlan map;
public ContainerZonePlan(IInventory playerInventory, TileZonePlan iZonePlan) {
super(0);
map = iZonePlan;
addSlotToContainer(new Slot(iZonePlan, 0, 233, 9));
addSlotToContainer(new SlotOutput(iZonePlan, 1, 233, 57));
// Player inventory
for (int l = 0; l < 3; l++) {
for (int k1 = 0; k1 < 9; k1++) {
addSlotToContainer(new Slot(playerInventory, k1 + l * 9 + 9, 88 + k1 * 18, 146 + l * 18));
}
}
for (int i1 = 0; i1 < 9; i1++) {
addSlotToContainer(new Slot(playerInventory, i1, 88 + i1 * 18, 204));
}
}
@Override
public boolean canInteractWith(EntityPlayer player) {
return true;
}
public void loadArea(final int index) {
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "loadArea", new CommandWriter() {
public void write(ByteBuf data) {
data.writeByte(index);
}
}));
}
public void saveArea(final int index) {
BuildCraftCore.instance.sendToServer(new PacketCommand(this, "saveArea", new CommandWriter() {
public void write(ByteBuf data) {
data.writeByte(index);
currentAreaSelection.writeData(data);
}
}));
}
@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
if (side.isClient()) {
if ("areaLoaded".equals(command)) {
currentAreaSelection = new ZonePlan();
currentAreaSelection.readData(stream);
gui.refreshSelectedArea();
} else if ("receiveImage".equals(command)) {
int size = stream.readUnsignedMedium();
for (int i = 0; i < size; ++i) {
mapTexture.colorMap[i] = 0xFF000000 | MapColor.mapColorArray[stream.readUnsignedByte()].colorValue;
}
}
} else if (side.isServer()) {
if ("loadArea".equals(command)) {
final int index = stream.readUnsignedByte();
BuildCraftCore.instance.sendToPlayer((EntityPlayer) sender, new PacketCommand(this, "areaLoaded", new CommandWriter() {
public void write(ByteBuf data) {
map.selectArea(index).writeData(data);
}
}));
} else if ("saveArea".equals(command)) {
final int index = stream.readUnsignedByte();
ZonePlan plan = new ZonePlan();
plan.readData(stream);
map.setArea(index, plan);
} else if ("computeMap".equals(command)) {
computeMap(stream.readInt(), stream.readInt(),
stream.readUnsignedShort(), stream.readUnsignedShort(),
stream.readUnsignedByte(), (EntityPlayer) sender);
} else if ("setName".equals(command)) {
map.mapName = NetworkUtils.readUTF(stream);
}
}
}
private void computeMap(int cx, int cz, int width, int height, int blocksPerPixel, EntityPlayer player) {
final byte[] textureData = new byte[width * height];
int startX = cx - width * blocksPerPixel / 2;
int startZ = cz - height * blocksPerPixel / 2;
for (int i = 0; i < width; ++i) {
for (int j = 0; j < height; ++j) {
int x = startX + i * blocksPerPixel;
int z = startZ + j * blocksPerPixel;
int ix = x - (map.chunkStartX << 4);
int iz = z - (map.chunkStartZ << 4);
if (ix >= 0 && iz >= 0 && ix < TileZonePlan.RESOLUTION && iz < TileZonePlan.RESOLUTION) {
textureData[i + j * width] = (byte) BuildCraftRobotics.manager.getWorld(map.getWorldObj())
.getColor(x, z);
}
}
}
BuildCraftCore.instance.sendToPlayer(player, new PacketCommand(this, "receiveImage", new CommandWriter() {
public void write(ByteBuf data) {
data.writeMedium(textureData.length);
data.writeBytes(textureData);
}
}));
}
}