Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Vazkii committed Oct 7, 2023
2 parents 002f5c1 + d6fa3e2 commit 3c3f92d
Show file tree
Hide file tree
Showing 16 changed files with 753 additions and 204 deletions.
10 changes: 5 additions & 5 deletions src/main/java/vazkii/quark/base/Quark.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ public class Quark {

public static final String MOD_ID = "quark";
public static final String ODDITIES_ID = "quarkoddities";

public static Quark instance;
public static CommonProxy proxy;

public static final Logger LOG = LogManager.getLogger(MOD_ID);

public Quark() {
instance = this;
proxy = DistExecutor.runForDist(() -> ClientProxy::new, () -> CommonProxy::new);

proxy = DistExecutor.safeRunForDist(() -> ClientProxy::new, () -> CommonProxy::new);
proxy.start();
}

}
8 changes: 4 additions & 4 deletions src/main/java/vazkii/quark/base/module/LoadModule.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package vazkii.quark.base.module;

import net.minecraftforge.api.distmarker.Dist;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import net.minecraftforge.api.distmarker.Dist;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoadModule {

ModuleCategory category();

String name() default "";
String description() default "";
String[] antiOverlap() default { };
Expand All @@ -21,5 +21,5 @@
Dist[] subscribeOn() default { Dist.CLIENT, Dist.DEDICATED_SERVER };

boolean enabledByDefault() default true;

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package vazkii.quark.base.module.config;

import java.util.HashMap;
import java.util.Map;

import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;
import net.minecraftforge.common.crafting.CraftingHelper;
import vazkii.quark.base.Quark;
import vazkii.quark.base.module.QuarkModule;
import vazkii.quark.base.module.sync.SyncedFlagHandler;
import vazkii.quark.base.recipe.ingredient.FlagIngredient;
import vazkii.quark.base.recipe.ingredient.PotionIngredient;

import java.util.*;

public final class ConfigFlagManager {

public static LootItemConditionType flagLootConditionType;

private final List<String> orderedFlags = new ArrayList<>();
private final Set<String> allFlags = new HashSet<>();
private final Map<String, Boolean> flags = new HashMap<>();
private boolean registered = false;

Expand All @@ -28,12 +30,14 @@ public void registerConfigBoundElements() {

CraftingHelper.register(new FlagRecipeCondition.Serializer(this, new ResourceLocation(Quark.MOD_ID, "flag")));
CraftingHelper.register(new FlagAdvancementCondition.Serializer(this, new ResourceLocation(Quark.MOD_ID, "advancement_flag")));

flagLootConditionType = new LootItemConditionType(new FlagLootCondition.FlagSerializer(this));
Registry.register(Registry.LOOT_CONDITION_TYPE, new ResourceLocation(Quark.MOD_ID, "flag"), flagLootConditionType);

CraftingHelper.register(new ResourceLocation(Quark.MOD_ID, "potion"), PotionIngredient.Serializer.INSTANCE);
CraftingHelper.register(new ResourceLocation(Quark.MOD_ID, "flag"), new FlagIngredient.Serializer(this));

SyncedFlagHandler.setupFlagManager(this, orderedFlags);
}

public void clear() {
Expand All @@ -42,10 +46,14 @@ public void clear() {

public void putFlag(QuarkModule module, String flag, boolean value) {
flags.put(flag, value && module.enabled);
if (!allFlags.contains(flag)) {
orderedFlags.add(flag);
allFlags.add(flag);
}
}

public void putEnabledFlag(QuarkModule module) {
flags.put(module.lowercaseName, module.enabled);
putFlag(module, module.lowercaseName, true);
}

public boolean isValidFlag(String flag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package vazkii.quark.base.module.config;

import net.minecraftforge.common.ForgeConfigSpec;
import org.apache.commons.lang3.text.WordUtils;
import org.jetbrains.annotations.Nullable;
import vazkii.quark.base.module.QuarkModule;
import vazkii.quark.base.module.config.type.IConfigType;
import vazkii.quark.base.module.hint.HintManager;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand All @@ -12,20 +19,12 @@
import java.util.function.Predicate;
import java.util.function.Supplier;

import org.apache.commons.lang3.text.WordUtils;
import org.jetbrains.annotations.Nullable;

import net.minecraftforge.common.ForgeConfigSpec;
import vazkii.quark.base.module.QuarkModule;
import vazkii.quark.base.module.config.type.IConfigType;
import vazkii.quark.base.module.hint.HintManager;

public final class ConfigObjectSerializer {

public static void serialize(IConfigBuilder builder, ConfigFlagManager flagManager, List<Runnable> callbacks, Object object) throws ReflectiveOperationException {
serialize(builder, flagManager, callbacks, object, object);
}

public static void serialize(IConfigBuilder builder, ConfigFlagManager flagManager, List<Runnable> callbacks, Object object, Object root) throws ReflectiveOperationException {
List<Field> fields = recursivelyGetFields(object.getClass());
for(Field f : fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class ConfigResolver {

private final ConfigFlagManager flagManager;

private List<Runnable> refreshRunnables = new LinkedList<>();
private final List<Runnable> refreshRunnables = new LinkedList<>();
private ModConfig config;

public ConfigResolver() {
Expand Down
75 changes: 75 additions & 0 deletions src/main/java/vazkii/quark/base/module/sync/SyncedFlagHandler.java
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);
}
}
45 changes: 29 additions & 16 deletions src/main/java/vazkii/quark/base/network/QuarkNetwork.java
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
package vazkii.quark.base.network;

import java.time.Instant;

import net.minecraft.network.chat.LastSeenMessages;
import net.minecraft.network.chat.MessageSignature;
import net.minecraft.network.protocol.Packet;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.NetworkDirection;
import net.minecraftforge.network.simple.SimpleChannel;
import vazkii.arl.network.IMessage;
import vazkii.arl.network.MessageSerializer;
import vazkii.arl.network.NetworkHandler;
import vazkii.quark.base.Quark;
import vazkii.quark.base.network.message.ChangeHotbarMessage;
import vazkii.quark.base.network.message.DoEmoteMessage;
import vazkii.quark.base.network.message.DoubleDoorMessage;
import vazkii.quark.base.network.message.EditSignMessage;
import vazkii.quark.base.network.message.HarvestMessage;
import vazkii.quark.base.network.message.InventoryTransferMessage;
import vazkii.quark.base.network.message.RequestEmoteMessage;
import vazkii.quark.base.network.message.ScrollOnBundleMessage;
import vazkii.quark.base.network.message.SetLockProfileMessage;
import vazkii.quark.base.network.message.ShareItemMessage;
import vazkii.quark.base.network.message.SortInventoryMessage;
import vazkii.quark.base.network.message.UpdateTridentMessage;
import vazkii.quark.base.network.message.*;
import vazkii.quark.base.network.message.experimental.PlaceVariantUpdateMessage;
import vazkii.quark.base.network.message.oddities.HandleBackpackMessage;
import vazkii.quark.base.network.message.oddities.MatrixEnchanterOperationMessage;
import vazkii.quark.base.network.message.oddities.ScrollCrateMessage;
import vazkii.quark.base.network.message.structural.*;

import java.time.Instant;
import java.util.BitSet;

public final class QuarkNetwork {

private static final int PROTOCOL_VERSION = 1;
private static final int PROTOCOL_VERSION = 2;

private static NetworkHandler network;

public static void setup() {
MessageSerializer.mapHandlers(Instant.class, (buf, field) -> buf.readInstant(), (buf, field, instant) -> buf.writeInstant(instant));
MessageSerializer.mapHandlers(MessageSignature.class, (buf, field) -> new MessageSignature(buf), (buf, field, signature) -> signature.write(buf));
MessageSerializer.mapHandlers(LastSeenMessages.Update.class, (buf, field) -> new LastSeenMessages.Update(buf), (buf, field, update) -> update.write(buf));
MessageSerializer.mapHandlers(BitSet.class, (buf, field) -> BitSet.valueOf(buf.readLongArray()), (buf, field, bitSet) -> buf.writeLongArray(bitSet.toLongArray()));

network = new NetworkHandler(Quark.MOD_ID, PROTOCOL_VERSION);

Expand All @@ -60,12 +53,32 @@ public static void setup() {

// Experimental
network.register(PlaceVariantUpdateMessage.class, NetworkDirection.PLAY_TO_SERVER);

// Clientbound
network.register(DoEmoteMessage.class, NetworkDirection.PLAY_TO_CLIENT);
network.register(EditSignMessage.class, NetworkDirection.PLAY_TO_CLIENT);
network.register(UpdateTridentMessage.class, NetworkDirection.PLAY_TO_CLIENT);

// Flag Syncing
network.register(S2CUpdateFlag.class, NetworkDirection.PLAY_TO_CLIENT);
network.register(C2SUpdateFlag.class, NetworkDirection.PLAY_TO_SERVER);
loginIndexedBuilder(S2CLoginFlag.class, 98, NetworkDirection.LOGIN_TO_CLIENT)
.decoder(S2CLoginFlag::new)
.buildLoginPacketList(S2CLoginFlag::generateRegistryPackets)
.add();
loginIndexedBuilder(C2SLoginFlag.class, 99, NetworkDirection.LOGIN_TO_SERVER)
.decoder(C2SLoginFlag::new)
.noResponse()
.add();
}

private static <MSG extends HandshakeMessage> SimpleChannel.MessageBuilder<MSG> loginIndexedBuilder(Class<MSG> clazz, int id, NetworkDirection direction) {
return network.channel.messageBuilder(clazz, id, direction)
.loginIndex(HandshakeMessage::getLoginIndex, HandshakeMessage::setLoginIndex)
.encoder(HandshakeMessage::encode)
.consumerNetworkThread((msg, context) -> {
return msg.consume(context.get(), network.channel::reply);
});
}

public static void sendToPlayer(IMessage msg, ServerPlayer player) {
Expand Down
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;
}

}
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());
}
}
Loading

0 comments on commit 3c3f92d

Please sign in to comment.