mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-21 04:32:24 +00:00
fix: preserve packet scheduler callback mutations
This commit is contained in:
1 parent
fa41ae0609
commit
052b84ffe0
3 files changed
+121
-32
No files matched your search
@@ -1,9 +1,11 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import codechicken.lib.data.MCDataOutput;
|
||||
import scala.Option;
|
||||
import scala.Tuple2;
|
||||
import scala.collection.mutable.HashMap;
|
||||
import scala.runtime.AbstractFunction1;
|
||||
import scala.runtime.BoxedUnit;
|
||||
|
||||
/**
|
||||
* Static class for packing update data. When a specific property of a part changes and needs sending to the client, a
|
||||
@@ -12,7 +14,7 @@ import codechicken.lib.data.MCDataOutput;
|
||||
*/
|
||||
public final class PacketScheduler {
|
||||
|
||||
private static final Map<TMultiPart, Long> map = new HashMap<>();
|
||||
private static final HashMap<TMultiPart, Long> map = new HashMap<>();
|
||||
|
||||
private PacketScheduler() {}
|
||||
|
||||
@@ -22,8 +24,8 @@ public final class PacketScheduler {
|
||||
throw new IllegalArgumentException("Cannot use PacketScheduler on a client world");
|
||||
}
|
||||
|
||||
Long current = map.get(part);
|
||||
map.put(part, (current == null ? 0L : current) | mask);
|
||||
Option<Long> current = map.get(part);
|
||||
map.put(part, (current.isEmpty() ? 0L : current.get()) | mask);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,32 +35,38 @@ public final class PacketScheduler {
|
||||
* 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());
|
||||
}
|
||||
// Callbacks may modify the schedule. Keep Scala's original traversal and visibility of pending masks.
|
||||
map.foreach(new AbstractFunction1<Tuple2<TMultiPart, Long>, BoxedUnit>() {
|
||||
|
||||
ipart.writeScheduled(mask, w);
|
||||
@Override
|
||||
public BoxedUnit apply(Tuple2<TMultiPart, Long> entry) {
|
||||
TMultiPart part = entry._1();
|
||||
long mask = entry._2();
|
||||
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);
|
||||
}
|
||||
return BoxedUnit.UNIT;
|
||||
}
|
||||
}
|
||||
});
|
||||
map.clear();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user