Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package net.minecraft.server;
2
3import java.util.ArrayList;
4import java.util.List;
5
6public class PlayerManager {
7
8 public List managedPlayers = new ArrayList();
9 private PlayerList b = new PlayerList();
10 private List c = new ArrayList();
11 private MinecraftServer server;
12 private int e;
13 private int f;
14 private final int[][] g = new int[][] { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 } };
15
16 public PlayerManager(MinecraftServer minecraftserver, int i, int j) {
17 if (j > 15) {
18 throw new IllegalArgumentException("Too big view radius!");
19 } else if (j < 3) {
20 throw new IllegalArgumentException("Too small view radius!");
21 } else {
22 this.f = j;
23 this.server = minecraftserver;
24 this.e = i;
25 }
26 }
27
28 public WorldServer a() {
29 return this.server.getWorldServer(this.e);
30 }
31
32 public void flush() {
33 for (int i = 0; i < this.c.size(); ++i) {
34 ((PlayerInstance) this.c.get(i)).a();
35 }
36
37 this.c.clear();
38 }
39
40 private PlayerInstance a(int i, int j, boolean flag) {
41 long k = (long) i + 2147483647L | (long) j + 2147483647L << 32;
42 PlayerInstance playerinstance = (PlayerInstance) this.b.a(k);
43
44 if (playerinstance == null && flag) {
45 playerinstance = new PlayerInstance(this, i, j);
46 this.b.a(k, playerinstance);
47 }
48
49 return playerinstance;
50 }
51
52 public void flagDirty(int i, int j, int k) {
53 int l = i >> 4;
54 int i1 = k >> 4;
55 PlayerInstance playerinstance = this.a(l, i1, false);
56
57 if (playerinstance != null) {
58 playerinstance.a(i & 15, j, k & 15);
59 }
60 }
61
62 public void addPlayer(EntityPlayer entityplayer) {
63 int i = (int) entityplayer.locX >> 4;
64 int j = (int) entityplayer.locZ >> 4;
65
66 entityplayer.d = entityplayer.locX;
67 entityplayer.e = entityplayer.locZ;
68 int k = 0;
69 int l = this.f;
70 int i1 = 0;
71 int j1 = 0;
72
73 this.a(i, j, true).a(entityplayer);
74
75 int k1;
76
77 for (k1 = 1; k1 <= l * 2; ++k1) {
78 for (int l1 = 0; l1 < 2; ++l1) {
79 int[] aint = this.g[k++ % 4];
80
81 for (int i2 = 0; i2 < k1; ++i2) {
82 i1 += aint[0];
83 j1 += aint[1];
84 this.a(i + i1, j + j1, true).a(entityplayer);
85 }
86 }
87 }
88
89 k %= 4;
90
91 for (k1 = 0; k1 < l * 2; ++k1) {
92 i1 += this.g[k][0];
93 j1 += this.g[k][1];
94 this.a(i + i1, j + j1, true).a(entityplayer);
95 }
96
97 this.managedPlayers.add(entityplayer);
98 }
99
100 public void removePlayer(EntityPlayer entityplayer) {
101 int i = (int) entityplayer.d >> 4;
102 int j = (int) entityplayer.e >> 4;
103
104 for (int k = i - this.f; k <= i + this.f; ++k) {
105 for (int l = j - this.f; l <= j + this.f; ++l) {
106 PlayerInstance playerinstance = this.a(k, l, false);
107
108 if (playerinstance != null) {
109 playerinstance.b(entityplayer);
110 }
111 }
112 }
113
114 this.managedPlayers.remove(entityplayer);
115 }
116
117 private boolean a(int i, int j, int k, int l) {
118 int i1 = i - k;
119 int j1 = j - l;
120
121 return i1 >= -this.f && i1 <= this.f ? j1 >= -this.f && j1 <= this.f : false;
122 }
123
124 public void movePlayer(EntityPlayer entityplayer) {
125 int i = (int) entityplayer.locX >> 4;
126 int j = (int) entityplayer.locZ >> 4;
127 double d0 = entityplayer.d - entityplayer.locX;
128 double d1 = entityplayer.e - entityplayer.locZ;
129 double d2 = d0 * d0 + d1 * d1;
130
131 if (d2 >= 64.0D) {
132 int k = (int) entityplayer.d >> 4;
133 int l = (int) entityplayer.e >> 4;
134 int i1 = i - k;
135 int j1 = j - l;
136
137 if (i1 != 0 || j1 != 0) {
138 for (int k1 = i - this.f; k1 <= i + this.f; ++k1) {
139 for (int l1 = j - this.f; l1 <= j + this.f; ++l1) {
140 if (!this.a(k1, l1, k, l)) {
141 this.a(k1, l1, true).a(entityplayer);
142 }
143
144 if (!this.a(k1 - i1, l1 - j1, i, j)) {
145 PlayerInstance playerinstance = this.a(k1 - i1, l1 - j1, false);
146
147 if (playerinstance != null) {
148 playerinstance.b(entityplayer);
149 }
150 }
151 }
152 }
153
154 entityplayer.d = entityplayer.locX;
155 entityplayer.e = entityplayer.locZ;
156
157 // CraftBukkit start - send nearest chunks first
158 if (i1 > 1 || i1 < -1 || j1 > 1 || j1 < -1) {
159 final int x = i;
160 final int z = j;
161 List<ChunkCoordIntPair> chunksToSend = entityplayer.chunkCoordIntPairQueue;
162
163 java.util.Collections.sort(chunksToSend, new java.util.Comparator<ChunkCoordIntPair>() {
164 public int compare(ChunkCoordIntPair a, ChunkCoordIntPair b) {
165 return Math.max(Math.abs(a.x - x), Math.abs(a.z - z)) - Math.max(Math.abs(b.x - x), Math.abs(b.z - z));
166 }
167 });
168 }
169 // CraftBukkit end
170 }
171 }
172 }
173
174 // Poseidon
175 public boolean a(EntityPlayer entityplayer, int i, int j) {
176 PlayerInstance playerchunk = this.a(i, j, false);
177
178 return playerchunk == null ? false : PlayerInstance.b(playerchunk).contains(entityplayer) && !entityplayer.chunkCoordIntPairQueue.contains(PlayerInstance.a(playerchunk));
179 }
180
181 public int getFurthestViewableBlock() {
182 return this.f * 16 - 16;
183 }
184
185 static PlayerList a(PlayerManager playermanager) {
186 return playermanager.b;
187 }
188
189 static List b(PlayerManager playermanager) {
190 return playermanager.c;
191 }
192}