/
ChunkCompressQueue.java
172 lines (150 loc) · 4.09 KB
/
ChunkCompressQueue.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package com.bergerkiller.bukkit.nolagg.chunks;
import java.util.Iterator;
import java.util.LinkedList;
import org.bukkit.Chunk;
import org.bukkit.entity.Player;
import com.bergerkiller.bukkit.common.utils.CommonUtil;
import com.bergerkiller.bukkit.common.utils.NativeUtil;
import net.minecraft.server.EntityPlayer;
import net.minecraft.server.Packet51MapChunk;
public class ChunkCompressQueue {
public ChunkCompressQueue(final ChunkSendQueue owner) {
this.owner = owner;
}
private final ChunkSendQueue owner;
private final LinkedList<Chunk> toCompress = new LinkedList<Chunk>();
private final LinkedList<ChunkSendCommand> toSend = new LinkedList<ChunkSendCommand>();
public void sort() {
synchronized (this.toCompress) {
this.owner.sort(this.toCompress);
}
synchronized (this.toSend) {
this.owner.sort(this.toSend);
}
}
public int getPendingSize() {
synchronized (this.toCompress) {
synchronized (this.toSend) {
return this.toCompress.size() + this.toSend.size();
}
}
}
public void clear() {
synchronized (this.toCompress) {
this.toCompress.clear();
}
synchronized (this.toSend) {
this.toSend.clear();
}
}
public boolean isAlive() {
EntityPlayer ep = NativeUtil.getNative(this.owner.player);
return ep.netServerHandler != null && !ep.netServerHandler.disconnected;
}
public boolean canSend() {
synchronized (this.toSend) {
return !this.toSend.isEmpty();
}
}
public boolean finishedCompressing() {
synchronized (this.toCompress) {
return this.toCompress.isEmpty();
}
}
public void enqueue(final Chunk chunk) {
if (chunk == null) {
return;
}
if (NoLaggChunks.useBufferedLoading) {
// Tell the threads to start the compression
synchronized (this.toCompress) {
this.toCompress.offer(chunk);
}
} else {
// Let the server itself deal with it
Packet51MapChunk packet = new Packet51MapChunk(NativeUtil.getNative(chunk), true, 0xffff);
packet.lowPriority = true;
this.enqueue(new ChunkSendCommand(packet, chunk));
}
}
public void enqueue(ChunkSendCommand sendCommand) {
if (sendCommand == null)
return;
if (!sendCommand.isValid())
return;
synchronized (this.toSend) {
this.toSend.offer(sendCommand);
}
}
public boolean remove(int x, int z) {
synchronized (this.toCompress) {
Iterator<Chunk> iter = this.toCompress.iterator();
while (iter.hasNext()) {
Chunk chunk = iter.next();
if (chunk.getX() == x && chunk.getZ() == z) {
iter.remove();
return true;
}
}
}
synchronized (this.toSend) {
Iterator<ChunkSendCommand> iter = this.toSend.iterator();
while (iter.hasNext()) {
ChunkSendCommand cmd = iter.next();
if (cmd.chunk.getX() == x && cmd.chunk.getZ() == z) {
iter.remove();
return true;
}
}
}
return false;
}
public boolean isNear(Chunk chunk) {
return this.isNear(chunk.getX(), chunk.getZ());
}
public boolean isNear(int x, int z) {
synchronized (this.owner) {
return this.owner.isNear(x, z, CommonUtil.view);
}
}
public Player owner() {
return this.owner.player;
}
public boolean hasChunk() {
synchronized (this.toCompress) {
return !this.toCompress.isEmpty();
}
}
public Chunk pollChunk() {
Chunk chunk;
synchronized (this.toCompress) {
if (this.toCompress.isEmpty())
return null;
chunk = this.toCompress.poll();
}
return chunk;
}
public ChunkSendCommand pollSendCommand() {
synchronized (this.toSend) {
if (this.toSend.isEmpty())
return null;
ChunkSendCommand cmd = this.toSend.poll();
if (this.isNear(cmd.chunk)) {
// In range of dynamic view?
return cmd;
} else {
this.owner.removeContained(cmd.chunk.getX(), cmd.chunk.getZ());
return this.pollSendCommand();
}
}
}
public boolean sendNext() {
ChunkSendCommand next = this.pollSendCommand();
if (next == null) {
return false;
}
next.send(this.owner);
this.owner.removeContained(next.chunk.getX(), next.chunk.getZ());
return true;
}
}