blob: 4590b5f5858ca93d4cfe92e0d464aea488700d79 (
plain)
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
|
package de.hype.bbsentials.chat;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.List;
import static de.hype.bbsentials.chat.Chat.sendPrivateMessageToSelf;
import static de.hype.bbsentials.chat.Chat.sendPrivateMessageToSelfText;
public class Sender {
private final List<String> sendQueue;
private final List<Double> sendQueueTiming;
private final List<Boolean> hidden;
public Sender() {
this.sendQueue = new ArrayList<>();
this.sendQueueTiming = new ArrayList<>();
this.hidden = new ArrayList<>();
startSendingThread();
}
public void addSendTask(String task, double timing) {
synchronized (sendQueue) {
sendPrivateMessageToSelfText(Text.literal(Formatting.GREEN + "Scheduled send-task (as " + sendQueueTiming.size() + " in line): " + task + " | Delay: " + timing));
sendQueueTiming.add(timing);
sendQueue.add(task);
hidden.add(false);
sendQueue.notify(); // Notify the waiting thread that a new String has been added
}
}
public void addHiddenSendTask(String task, double timing) {
synchronized (sendQueue) {
sendQueueTiming.add(timing);
sendQueue.add(task);
hidden.add(true);
sendQueue.notify(); // Notify the waiting thread that a new String has been added
}
}
public void addImmediateSendTask(String task) {
synchronized (sendQueue) {
sendQueueTiming.add(0, 0.0);
sendQueue.add(0, task);
hidden.add(false);
sendQueue.notify(); // Notify the waiting thread that a new String has been added
}
}
public void addSendTask(String task) {
addSendTask(task, 1);
}
public void startSendingThread() {
Thread sendingThread = new Thread(new SendingRunnable());
sendingThread.start();
}
private class SendingRunnable implements Runnable {
@Override
public void run() {
while (true) {
String task = getNextTask();
if (task != null) {
send(task, sendQueueTiming.remove(0), hidden.remove(0));
}
else {
synchronized (sendQueue) {
try {
sendQueue.wait(); // Wait for new Send
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
}
private String getNextTask() {
synchronized (sendQueue) {
if (!sendQueue.isEmpty()) {
return sendQueue.remove(0);
}
return null;
}
}
private void send(String toSend, double timing, boolean hidden) {
try {
Thread.sleep((long) (timing * 1000)); // Simulate the send operation
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
MinecraftClient.getInstance().getNetworkHandler().sendChatMessage(toSend);
if (!hidden) {
sendPrivateMessageToSelf("Sent Command to Server: " + toSend);
}
}
}
}
|