mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-21 20:52:21 +00:00
refactor: port PacketScheduler to Java
IScheduledPacketPart is descriptor-identical and schedulePacket is unchanged. TScheduledPacketPart.read stays abstract: TMultiPart declares read, so a default would be shadowed and a part would silently read a description instead of a mask. The dispatch that lived in the $class helper is now the interface static readMask, with the one-line forwarder documented on read. writeScheduled and readScheduled became defaults. TMultiPart declares neither, and empty bodies are what mixing the Scala trait in already gave a Scala implementor -- only Java implementors ever had to write them out. sendScheduled is public because the reference's private[multipart] reaches the handler package and Java has no equivalent scope. An unsupported mask width now throws IllegalArgumentException rather than scala.MatchError; both are RuntimeException and the map is still left uncleared on that path, as before. Dropped PacketScheduler$, TScheduledPacketPart$class and two closure classes, none referenced in bytecode or by reflective name. 118 plain-JVM tests and 18 Forge server tests pass.
This commit is contained in:
1 parent
3ad25f56cb
commit
40bb1b71b7
9 files changed
+267
-123
No files matched your search
@@ -0,0 +1,64 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import codechicken.lib.data.MCDataOutput;
|
||||
|
||||
/**
|
||||
* Static class for packing update data. When a specific property of a part changes and needs sending to the client, a
|
||||
* bit can be set in the mask. This bit can then be checked in the writeScheduled callback. This prevents sending
|
||||
* multiple packets if the same property updates more than once per tick.
|
||||
*/
|
||||
public final class PacketScheduler {
|
||||
|
||||
private static final Map<TMultiPart, Long> map = new HashMap<>();
|
||||
|
||||
private PacketScheduler() {}
|
||||
|
||||
/** Add bits to the current update mask for part. (binary OR) */
|
||||
public static void schedulePacket(TMultiPart part, long mask) {
|
||||
if (part.world().isRemote) {
|
||||
throw new IllegalArgumentException("Cannot use PacketScheduler on a client world");
|
||||
}
|
||||
|
||||
Long current = map.get(part);
|
||||
map.put(part, (current == null ? 0L : current) | mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes every scheduled part and clears the schedule.
|
||||
* <p>
|
||||
* The reference declared this {@code private[multipart]}, which reaches the handler package. Java has no equivalent
|
||||
* scope, so it is public.
|
||||
*/
|
||||
public static void sendScheduled() {
|
||||
for (Map.Entry<TMultiPart, Long> e : map.entrySet()) {
|
||||
TMultiPart part = e.getKey();
|
||||
long mask = e.getValue();
|
||||
if (part.tile() != null) {
|
||||
IScheduledPacketPart ipart = (IScheduledPacketPart) part;
|
||||
MCDataOutput w = part.getWriteStream();
|
||||
switch (ipart.maskWidth()) {
|
||||
case 1:
|
||||
w.writeByte((int) mask);
|
||||
break;
|
||||
case 2:
|
||||
w.writeShort((int) mask);
|
||||
break;
|
||||
case 4:
|
||||
w.writeInt((int) mask);
|
||||
break;
|
||||
case 8:
|
||||
w.writeLong(mask);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Invalid maskWidth: " + ipart.maskWidth());
|
||||
}
|
||||
|
||||
ipart.writeScheduled(mask, w);
|
||||
}
|
||||
}
|
||||
map.clear();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user