-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
16 changed files
with
753 additions
and
204 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
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
75 changes: 75 additions & 0 deletions
75
src/main/java/vazkii/quark/base/module/sync/SyncedFlagHandler.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,75 @@ | ||
package vazkii.quark.base.module.sync; | ||
|
||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.network.PacketListener; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraftforge.api.distmarker.Dist; | ||
import net.minecraftforge.api.distmarker.OnlyIn; | ||
import vazkii.quark.base.module.config.ConfigFlagManager; | ||
import vazkii.quark.base.network.QuarkNetwork; | ||
import vazkii.quark.base.network.message.structural.S2CUpdateFlag; | ||
|
||
import java.util.*; | ||
|
||
public class SyncedFlagHandler { | ||
private static ConfigFlagManager flagManager; | ||
private static List<String> allFlags; | ||
|
||
public static void setupFlagManager(ConfigFlagManager manager, List<String> flags) { | ||
flagManager = manager; | ||
allFlags = flags; | ||
} | ||
|
||
public static BitSet compileFlagInfo() { | ||
BitSet set = new BitSet(); | ||
int i = 0; | ||
for (String flag : allFlags) set.set(i++, flagManager.getFlag(flag)); | ||
|
||
return set; | ||
} | ||
|
||
private static Set<String> decodeFlags(BitSet bitSet) { | ||
Set<String> enabledFlags = new HashSet<>(); | ||
|
||
for (int i = bitSet.nextSetBit(0); i >= 0; i = bitSet.nextSetBit(i + 1)) { | ||
enabledFlags.add(allFlags.get(i)); | ||
} | ||
|
||
return enabledFlags; | ||
} | ||
|
||
public static void receiveFlagInfoFromPlayer(ServerPlayer player, BitSet bitSet) { | ||
flagsFromPlayers.put(player, decodeFlags(bitSet)); | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
public static void receiveFlagInfoFromServer(BitSet bitSet) { | ||
flagsFromServer.put(Minecraft.getInstance().getConnection(), decodeFlags(bitSet)); | ||
} | ||
|
||
public static void sendFlagInfoToPlayers() { | ||
QuarkNetwork.sendToPlayers(new S2CUpdateFlag(), flagsFromPlayers.keySet()); | ||
} | ||
|
||
private static final WeakHashMap<PacketListener, Set<String>> flagsFromServer = new WeakHashMap<>(); | ||
private static final WeakHashMap<ServerPlayer, Set<String>> flagsFromPlayers = new WeakHashMap<>(); | ||
|
||
public static boolean getFlagForPlayer(ServerPlayer player, String flag) { | ||
Set<String> enabledFlags = flagsFromPlayers.get(player); | ||
if (enabledFlags == null) | ||
return flagManager.getFlag(flag); | ||
|
||
return enabledFlags.contains(flag); | ||
} | ||
|
||
@OnlyIn(Dist.CLIENT) | ||
public static boolean getFlagForServer(String flag) { | ||
for (PacketListener listener : flagsFromServer.keySet()) { | ||
Set<String> enabledFlags = flagsFromServer.get(listener); | ||
if (enabledFlags != null) | ||
return enabledFlags.contains(flag); | ||
} | ||
|
||
return flagManager.getFlag(flag); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/vazkii/quark/base/network/message/structural/C2SLoginFlag.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,33 @@ | ||
package vazkii.quark.base.network.message.structural; | ||
|
||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraftforge.network.NetworkEvent; | ||
import vazkii.quark.base.module.sync.SyncedFlagHandler; | ||
|
||
import java.util.BitSet; | ||
import java.util.function.BiConsumer; | ||
|
||
public class C2SLoginFlag extends HandshakeMessage { | ||
|
||
public BitSet flags; | ||
|
||
public C2SLoginFlag() { | ||
flags = SyncedFlagHandler.compileFlagInfo(); | ||
} | ||
|
||
public C2SLoginFlag(FriendlyByteBuf buf) { | ||
this.flags = BitSet.valueOf(buf.readLongArray()); | ||
} | ||
|
||
@Override | ||
public void encode(FriendlyByteBuf buf) { | ||
buf.writeLongArray(flags.toLongArray()); | ||
} | ||
|
||
@Override | ||
public boolean consume(NetworkEvent.Context context, BiConsumer<HandshakeMessage, NetworkEvent.Context> reply) { | ||
SyncedFlagHandler.receiveFlagInfoFromPlayer(context.getSender(), flags); | ||
return true; | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/vazkii/quark/base/network/message/structural/C2SUpdateFlag.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,34 @@ | ||
package vazkii.quark.base.network.message.structural; | ||
|
||
import net.minecraftforge.network.NetworkEvent; | ||
import vazkii.arl.network.IMessage; | ||
import vazkii.quark.base.module.sync.SyncedFlagHandler; | ||
|
||
import java.io.Serial; | ||
import java.util.BitSet; | ||
|
||
public class C2SUpdateFlag implements IMessage { | ||
|
||
@Serial | ||
private static final long serialVersionUID = 7483741039149504284L; | ||
|
||
public BitSet flags; | ||
|
||
@Override | ||
public boolean receive(NetworkEvent.Context context) { | ||
SyncedFlagHandler.receiveFlagInfoFromPlayer(context.getSender(), flags); | ||
return true; | ||
} | ||
|
||
public C2SUpdateFlag() { | ||
// NO-OP | ||
} | ||
|
||
private C2SUpdateFlag(BitSet flags) { | ||
this.flags = flags; | ||
} | ||
|
||
public static C2SUpdateFlag createPacket() { | ||
return new C2SUpdateFlag(SyncedFlagHandler.compileFlagInfo()); | ||
} | ||
} |
Oops, something went wrong.