-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add some additional sync handlers
- Loading branch information
1 parent
935c477
commit 6384adc
Showing
9 changed files
with
401 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/dev/galacticraft/machinelib/impl/menu/sync/BitsPacketSerializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (c) 2021-2024 Team Galacticraft | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.galacticraft.machinelib.impl.menu.sync; | ||
|
||
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable; | ||
import io.netty.buffer.ByteBuf; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public record BitsPacketSerializable(int len, boolean[] source, boolean[] dest) implements DeltaPacketSerializable<ByteBuf, boolean[]> { | ||
public BitsPacketSerializable { | ||
assert len == source.length; | ||
assert len == dest.length; | ||
} | ||
|
||
@Override | ||
public boolean hasChanged(boolean[] previous) { | ||
for (int i = 0; i < this.len; i++) { | ||
if (previous[i] != this.source[i]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void copyInto(boolean[] other) { | ||
System.arraycopy(this.source, 0, other, 0, this.len); | ||
} | ||
|
||
@Override | ||
public void readPacket(@NotNull ByteBuf buf) { | ||
int bytes = (this.len / 8) + 1; | ||
for (int i = 0; i < bytes; i++) { | ||
byte b = buf.readByte(); | ||
for (int j = 0; j < 8 && i * 8 + j < this.len; j++) { | ||
this.dest[i * 8 + j] = (b & (0b1 << j)) != 0; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void writePacket(@NotNull ByteBuf buf) { | ||
int bytes = (this.len / 8) + 1; | ||
for (int i = 0; i < bytes; i++) { | ||
byte b = 0; | ||
for (int j = 0; j < 8 && i * 8 + j < this.len; j++) { | ||
if (this.source[i * 8 + j]) { | ||
b |= (byte) (0b1 << j); | ||
} | ||
} | ||
buf.writeByte(b); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean[] createEquivalent() { | ||
return new boolean[this.len]; | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/dev/galacticraft/machinelib/impl/menu/sync/BytePacketSerializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2021-2024 Team Galacticraft | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.galacticraft.machinelib.impl.menu.sync; | ||
|
||
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable; | ||
import io.netty.buffer.ByteBuf; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.function.IntConsumer; | ||
import java.util.function.IntSupplier; | ||
|
||
public record BytePacketSerializable(IntSupplier getter, | ||
IntConsumer setter) implements DeltaPacketSerializable<ByteBuf, int[]> { | ||
@Override | ||
public boolean hasChanged(int[] previous) { | ||
return previous[0] != this.getter.getAsInt(); | ||
} | ||
|
||
@Override | ||
public void copyInto(int[] other) { | ||
other[0] = this.getter.getAsInt(); | ||
} | ||
|
||
@Override | ||
public void readPacket(@NotNull ByteBuf buf) { | ||
this.setter.accept(buf.readByte()); | ||
} | ||
|
||
@Override | ||
public void writePacket(@NotNull ByteBuf buf) { | ||
buf.writeByte(this.getter.getAsInt()); | ||
} | ||
|
||
@Override | ||
public int[] createEquivalent() { | ||
return new int[1]; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/dev/galacticraft/machinelib/impl/menu/sync/DoublePacketSerializable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* Copyright (c) 2021-2024 Team Galacticraft | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
|
||
package dev.galacticraft.machinelib.impl.menu.sync; | ||
|
||
import dev.galacticraft.machinelib.api.misc.DeltaPacketSerializable; | ||
import io.netty.buffer.ByteBuf; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.util.function.DoubleConsumer; | ||
import java.util.function.DoubleSupplier; | ||
|
||
public record DoublePacketSerializable(DoubleSupplier getter, | ||
DoubleConsumer setter) implements DeltaPacketSerializable<ByteBuf, double[]> { | ||
@Override | ||
public boolean hasChanged(double[] previous) { | ||
return previous[0] != this.getter.getAsDouble(); | ||
} | ||
|
||
@Override | ||
public void copyInto(double[] other) { | ||
other[0] = this.getter.getAsDouble(); | ||
} | ||
|
||
@Override | ||
public void readPacket(@NotNull ByteBuf buf) { | ||
this.setter.accept(buf.readDouble()); | ||
} | ||
|
||
@Override | ||
public void writePacket(@NotNull ByteBuf buf) { | ||
buf.writeDouble(this.getter.getAsDouble()); | ||
} | ||
|
||
@Override | ||
public double[] createEquivalent() { | ||
return new double[1]; | ||
} | ||
} |
Oops, something went wrong.