| 1 | package net.minecraft.world.storage;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import java.util.List;
|
| 6 | import java.util.Map;
|
| 7 | import net.minecraft.entity.item.EntityItemFrame;
|
| 8 | import net.minecraft.entity.player.EntityPlayer;
|
| 9 | import net.minecraft.item.ItemStack;
|
| 10 | import net.minecraft.nbt.NBTTagCompound;
|
| 11 | import net.minecraft.nbt.NBTTagList;
|
| 12 | import net.minecraft.network.Packet;
|
| 13 | import net.minecraft.network.play.server.S34PacketMaps;
|
| 14 | import net.minecraft.util.BlockPos;
|
| 15 | import net.minecraft.util.MathHelper;
|
| 16 | import net.minecraft.util.Vec4b;
|
| 17 | import net.minecraft.world.World;
|
| 18 | import net.minecraft.world.WorldSavedData;
|
| 19 |
|
| 20 | public class MapData extends WorldSavedData
|
| 21 | {
|
| 22 | public int xCenter;
|
| 23 | public int zCenter;
|
| 24 | public byte dimension;
|
| 25 | public byte scale;
|
| 26 |
|
| 27 | /** colours */
|
| 28 | public byte[] colors = new byte[16384];
|
| 29 | public List<MapData.MapInfo> playersArrayList = Lists.<MapData.MapInfo>newArrayList();
|
| 30 | private Map<EntityPlayer, MapData.MapInfo> playersHashMap = Maps.<EntityPlayer, MapData.MapInfo>newHashMap();
|
| 31 | public Map<String, Vec4b> mapDecorations = Maps.<String, Vec4b>newLinkedHashMap();
|
| 32 |
|
| 33 | public MapData(String mapname)
|
| 34 | {
|
| 35 | super(mapname);
|
| 36 | }
|
| 37 |
|
| 38 | public void calculateMapCenter(double x, double z, int mapScale)
|
| 39 | {
|
| 40 | int i = 128 * (1 << mapScale);
|
| 41 | int j = MathHelper.floor_double((x + 64.0D) / (double)i);
|
| 42 | int k = MathHelper.floor_double((z + 64.0D) / (double)i);
|
| 43 | this.xCenter = j * i + i / 2 - 64;
|
| 44 | this.zCenter = k * i + i / 2 - 64;
|
| 45 | }
|
| 46 |
|
| 47 | /**
|
| 48 | * reads in data from the NBTTagCompound into this MapDataBase
|
| 49 | */
|
| 50 | public void readFromNBT(NBTTagCompound nbt)
|
| 51 | {
|
| 52 | this.dimension = nbt.getByte("dimension");
|
| 53 | this.xCenter = nbt.getInteger("xCenter");
|
| 54 | this.zCenter = nbt.getInteger("zCenter");
|
| 55 | this.scale = nbt.getByte("scale");
|
| 56 | this.scale = (byte)MathHelper.clamp_int(this.scale, 0, 4);
|
| 57 | int i = nbt.getShort("width");
|
| 58 | int j = nbt.getShort("height");
|
| 59 |
|
| 60 | if (i == 128 && j == 128)
|
| 61 | {
|
| 62 | this.colors = nbt.getByteArray("colors");
|
| 63 | }
|
| 64 | else
|
| 65 | {
|
| 66 | byte[] abyte = nbt.getByteArray("colors");
|
| 67 | this.colors = new byte[16384];
|
| 68 | int k = (128 - i) / 2;
|
| 69 | int l = (128 - j) / 2;
|
| 70 |
|
| 71 | for (int i1 = 0; i1 < j; ++i1)
|
| 72 | {
|
| 73 | int j1 = i1 + l;
|
| 74 |
|
| 75 | if (j1 >= 0 || j1 < 128)
|
| 76 | {
|
| 77 | for (int k1 = 0; k1 < i; ++k1)
|
| 78 | {
|
| 79 | int l1 = k1 + k;
|
| 80 |
|
| 81 | if (l1 >= 0 || l1 < 128)
|
| 82 | {
|
| 83 | this.colors[l1 + j1 * 128] = abyte[k1 + i1 * i];
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 | }
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | /**
|
| 92 | * write data to NBTTagCompound from this MapDataBase, similar to Entities and TileEntities
|
| 93 | */
|
| 94 | public void writeToNBT(NBTTagCompound nbt)
|
| 95 | {
|
| 96 | nbt.setByte("dimension", this.dimension);
|
| 97 | nbt.setInteger("xCenter", this.xCenter);
|
| 98 | nbt.setInteger("zCenter", this.zCenter);
|
| 99 | nbt.setByte("scale", this.scale);
|
| 100 | nbt.setShort("width", (short)128);
|
| 101 | nbt.setShort("height", (short)128);
|
| 102 | nbt.setByteArray("colors", this.colors);
|
| 103 | }
|
| 104 |
|
| 105 | /**
|
| 106 | * Adds the player passed to the list of visible players and checks to see which players are visible
|
| 107 | */
|
| 108 | public void updateVisiblePlayers(EntityPlayer player, ItemStack mapStack)
|
| 109 | {
|
| 110 | if (!this.playersHashMap.containsKey(player))
|
| 111 | {
|
| 112 | MapData.MapInfo mapdata$mapinfo = new MapData.MapInfo(player);
|
| 113 | this.playersHashMap.put(player, mapdata$mapinfo);
|
| 114 | this.playersArrayList.add(mapdata$mapinfo);
|
| 115 | }
|
| 116 |
|
| 117 | if (!player.inventory.hasItemStack(mapStack))
|
| 118 | {
|
| 119 | this.mapDecorations.remove(player.getName());
|
| 120 | }
|
| 121 |
|
| 122 | for (int i = 0; i < this.playersArrayList.size(); ++i)
|
| 123 | {
|
| 124 | MapData.MapInfo mapdata$mapinfo1 = (MapData.MapInfo)this.playersArrayList.get(i);
|
| 125 |
|
| 126 | if (!mapdata$mapinfo1.entityplayerObj.isDead && (mapdata$mapinfo1.entityplayerObj.inventory.hasItemStack(mapStack) || mapStack.isOnItemFrame()))
|
| 127 | {
|
| 128 | if (!mapStack.isOnItemFrame() && mapdata$mapinfo1.entityplayerObj.dimension == this.dimension)
|
| 129 | {
|
| 130 | this.updateDecorations(0, mapdata$mapinfo1.entityplayerObj.worldObj, mapdata$mapinfo1.entityplayerObj.getName(), mapdata$mapinfo1.entityplayerObj.posX, mapdata$mapinfo1.entityplayerObj.posZ, (double)mapdata$mapinfo1.entityplayerObj.rotationYaw);
|
| 131 | }
|
| 132 | }
|
| 133 | else
|
| 134 | {
|
| 135 | this.playersHashMap.remove(mapdata$mapinfo1.entityplayerObj);
|
| 136 | this.playersArrayList.remove(mapdata$mapinfo1);
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | if (mapStack.isOnItemFrame())
|
| 141 | {
|
| 142 | EntityItemFrame entityitemframe = mapStack.getItemFrame();
|
| 143 | BlockPos blockpos = entityitemframe.getHangingPosition();
|
| 144 | this.updateDecorations(1, player.worldObj, "frame-" + entityitemframe.getEntityId(), (double)blockpos.getX(), (double)blockpos.getZ(), (double)(entityitemframe.facingDirection.getHorizontalIndex() * 90));
|
| 145 | }
|
| 146 |
|
| 147 | if (mapStack.hasTagCompound() && mapStack.getTagCompound().hasKey("Decorations", 9))
|
| 148 | {
|
| 149 | NBTTagList nbttaglist = mapStack.getTagCompound().getTagList("Decorations", 10);
|
| 150 |
|
| 151 | for (int j = 0; j < nbttaglist.tagCount(); ++j)
|
| 152 | {
|
| 153 | NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(j);
|
| 154 |
|
| 155 | if (!this.mapDecorations.containsKey(nbttagcompound.getString("id")))
|
| 156 | {
|
| 157 | this.updateDecorations(nbttagcompound.getByte("type"), player.worldObj, nbttagcompound.getString("id"), nbttagcompound.getDouble("x"), nbttagcompound.getDouble("z"), nbttagcompound.getDouble("rot"));
|
| 158 | }
|
| 159 | }
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | private void updateDecorations(int type, World worldIn, String entityIdentifier, double worldX, double worldZ, double rotation)
|
| 164 | {
|
| 165 | int i = 1 << this.scale;
|
| 166 | float f = (float)(worldX - (double)this.xCenter) / (float)i;
|
| 167 | float f1 = (float)(worldZ - (double)this.zCenter) / (float)i;
|
| 168 | byte b0 = (byte)((int)((double)(f * 2.0F) + 0.5D));
|
| 169 | byte b1 = (byte)((int)((double)(f1 * 2.0F) + 0.5D));
|
| 170 | int j = 63;
|
| 171 | byte b2;
|
| 172 |
|
| 173 | if (f >= (float)(-j) && f1 >= (float)(-j) && f <= (float)j && f1 <= (float)j)
|
| 174 | {
|
| 175 | rotation = rotation + (rotation < 0.0D ? -8.0D : 8.0D);
|
| 176 | b2 = (byte)((int)(rotation * 16.0D / 360.0D));
|
| 177 |
|
| 178 | if (this.dimension < 0)
|
| 179 | {
|
| 180 | int k = (int)(worldIn.getWorldInfo().getWorldTime() / 10L);
|
| 181 | b2 = (byte)(k * k * 34187121 + k * 121 >> 15 & 15);
|
| 182 | }
|
| 183 | }
|
| 184 | else
|
| 185 | {
|
| 186 | if (Math.abs(f) >= 320.0F || Math.abs(f1) >= 320.0F)
|
| 187 | {
|
| 188 | this.mapDecorations.remove(entityIdentifier);
|
| 189 | return;
|
| 190 | }
|
| 191 |
|
| 192 | type = 6;
|
| 193 | b2 = 0;
|
| 194 |
|
| 195 | if (f <= (float)(-j))
|
| 196 | {
|
| 197 | b0 = (byte)((int)((double)(j * 2) + 2.5D));
|
| 198 | }
|
| 199 |
|
| 200 | if (f1 <= (float)(-j))
|
| 201 | {
|
| 202 | b1 = (byte)((int)((double)(j * 2) + 2.5D));
|
| 203 | }
|
| 204 |
|
| 205 | if (f >= (float)j)
|
| 206 | {
|
| 207 | b0 = (byte)(j * 2 + 1);
|
| 208 | }
|
| 209 |
|
| 210 | if (f1 >= (float)j)
|
| 211 | {
|
| 212 | b1 = (byte)(j * 2 + 1);
|
| 213 | }
|
| 214 | }
|
| 215 |
|
| 216 | this.mapDecorations.put(entityIdentifier, new Vec4b((byte)type, b0, b1, b2));
|
| 217 | }
|
| 218 |
|
| 219 | public Packet getMapPacket(ItemStack mapStack, World worldIn, EntityPlayer player)
|
| 220 | {
|
| 221 | MapData.MapInfo mapdata$mapinfo = (MapData.MapInfo)this.playersHashMap.get(player);
|
| 222 | return mapdata$mapinfo == null ? null : mapdata$mapinfo.getPacket(mapStack);
|
| 223 | }
|
| 224 |
|
| 225 | public void updateMapData(int x, int y)
|
| 226 | {
|
| 227 | super.markDirty();
|
| 228 |
|
| 229 | for (MapData.MapInfo mapdata$mapinfo : this.playersArrayList)
|
| 230 | {
|
| 231 | mapdata$mapinfo.update(x, y);
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | public MapData.MapInfo getMapInfo(EntityPlayer player)
|
| 236 | {
|
| 237 | MapData.MapInfo mapdata$mapinfo = (MapData.MapInfo)this.playersHashMap.get(player);
|
| 238 |
|
| 239 | if (mapdata$mapinfo == null)
|
| 240 | {
|
| 241 | mapdata$mapinfo = new MapData.MapInfo(player);
|
| 242 | this.playersHashMap.put(player, mapdata$mapinfo);
|
| 243 | this.playersArrayList.add(mapdata$mapinfo);
|
| 244 | }
|
| 245 |
|
| 246 | return mapdata$mapinfo;
|
| 247 | }
|
| 248 |
|
| 249 | public class MapInfo
|
| 250 | {
|
| 251 | public final EntityPlayer entityplayerObj;
|
| 252 | private boolean field_176105_d = true;
|
| 253 | private int minX = 0;
|
| 254 | private int minY = 0;
|
| 255 | private int maxX = 127;
|
| 256 | private int maxY = 127;
|
| 257 | private int field_176109_i;
|
| 258 | public int field_82569_d;
|
| 259 |
|
| 260 | public MapInfo(EntityPlayer player)
|
| 261 | {
|
| 262 | this.entityplayerObj = player;
|
| 263 | }
|
| 264 |
|
| 265 | public Packet getPacket(ItemStack stack)
|
| 266 | {
|
| 267 | if (this.field_176105_d)
|
| 268 | {
|
| 269 | this.field_176105_d = false;
|
| 270 | return new S34PacketMaps(stack.getMetadata(), MapData.this.scale, MapData.this.mapDecorations.values(), MapData.this.colors, this.minX, this.minY, this.maxX + 1 - this.minX, this.maxY + 1 - this.minY);
|
| 271 | }
|
| 272 | else
|
| 273 | {
|
| 274 | return this.field_176109_i++ % 5 == 0 ? new S34PacketMaps(stack.getMetadata(), MapData.this.scale, MapData.this.mapDecorations.values(), MapData.this.colors, 0, 0, 0, 0) : null;
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | public void update(int x, int y)
|
| 279 | {
|
| 280 | if (this.field_176105_d)
|
| 281 | {
|
| 282 | this.minX = Math.min(this.minX, x);
|
| 283 | this.minY = Math.min(this.minY, y);
|
| 284 | this.maxX = Math.max(this.maxX, x);
|
| 285 | this.maxY = Math.max(this.maxY, y);
|
| 286 | }
|
| 287 | else
|
| 288 | {
|
| 289 | this.field_176105_d = true;
|
| 290 | this.minX = x;
|
| 291 | this.minY = y;
|
| 292 | this.maxX = x;
|
| 293 | this.maxY = y;
|
| 294 | }
|
| 295 | }
|
| 296 | }
|
| 297 | }
|
| 298 |
|