2022-04-17 20:43:07 +08:00
|
|
|
package emu.grasscutter.game;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2022-04-27 12:21:57 +08:00
|
|
|
import emu.grasscutter.net.packet.BasePacket;
|
2022-04-17 20:43:07 +08:00
|
|
|
import emu.grasscutter.net.proto.ForwardTypeOuterClass.ForwardType;
|
|
|
|
|
|
|
|
public class InvokeHandler<T> {
|
|
|
|
private final List<T> entryListForwardAll;
|
|
|
|
private final List<T> entryListForwardAllExceptCur;
|
|
|
|
private final List<T> entryListForwardHost;
|
2022-04-27 12:21:57 +08:00
|
|
|
private final Class<? extends BasePacket> packetClass;
|
2022-04-17 20:43:07 +08:00
|
|
|
|
2022-04-27 12:21:57 +08:00
|
|
|
public InvokeHandler(Class<? extends BasePacket> packetClass) {
|
2022-04-17 20:43:07 +08:00
|
|
|
this.entryListForwardAll = new ArrayList<>();
|
|
|
|
this.entryListForwardAllExceptCur = new ArrayList<>();
|
|
|
|
this.entryListForwardHost = new ArrayList<>();
|
|
|
|
this.packetClass = packetClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
public synchronized void addEntry(ForwardType forward, T entry) {
|
|
|
|
switch (forward) {
|
2022-04-27 05:44:30 +08:00
|
|
|
case FORWARD_TO_ALL -> entryListForwardAll.add(entry);
|
|
|
|
case FORWARD_TO_ALL_EXCEPT_CUR, FORWARD_TO_ALL_EXIST_EXCEPT_CUR -> entryListForwardAllExceptCur.add(entry);
|
|
|
|
case FORWARD_TO_HOST -> entryListForwardHost.add(entry);
|
|
|
|
default -> {
|
|
|
|
}
|
2022-04-17 20:43:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 12:21:57 +08:00
|
|
|
public synchronized void update(Player player) {
|
2022-04-17 20:43:07 +08:00
|
|
|
if (player.getWorld() == null) {
|
|
|
|
this.entryListForwardAll.clear();
|
|
|
|
this.entryListForwardAllExceptCur.clear();
|
|
|
|
this.entryListForwardHost.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (entryListForwardAll.size() > 0) {
|
2022-04-27 12:21:57 +08:00
|
|
|
BasePacket packet = packetClass.getDeclaredConstructor(List.class).newInstance(this.entryListForwardAll);
|
2022-04-19 00:39:29 +08:00
|
|
|
player.getScene().broadcastPacket(packet);
|
2022-04-17 20:43:07 +08:00
|
|
|
this.entryListForwardAll.clear();
|
|
|
|
}
|
|
|
|
if (entryListForwardAllExceptCur.size() > 0) {
|
2022-04-27 12:21:57 +08:00
|
|
|
BasePacket packet = packetClass.getDeclaredConstructor(List.class).newInstance(this.entryListForwardAllExceptCur);
|
2022-04-19 00:39:29 +08:00
|
|
|
player.getScene().broadcastPacketToOthers(player, packet);
|
2022-04-17 20:43:07 +08:00
|
|
|
this.entryListForwardAllExceptCur.clear();
|
|
|
|
}
|
|
|
|
if (entryListForwardHost.size() > 0) {
|
2022-04-27 12:21:57 +08:00
|
|
|
BasePacket packet = packetClass.getDeclaredConstructor(List.class).newInstance(this.entryListForwardHost);
|
2022-04-17 20:43:07 +08:00
|
|
|
player.getWorld().getHost().sendPacket(packet);
|
|
|
|
this.entryListForwardHost.clear();
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|