Skip to content

RevelationRegistry (de)serialization changes #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
strategy:
matrix:
# Use these Java versions
java: [17] # Current Java LTS & minimum supported by Minecraft
java: [21] # Current Java LTS & minimum supported by Minecraft
# Test on both Linux and Windows
os: [ubuntu-20.04, windows-latest]
os: [ubuntu-latest, windows-latest]

runs-on: ${{ matrix.os }}

Expand All @@ -38,7 +38,7 @@ jobs:

- name: capture build artifacts
# Only capture artifacts from one OS (Linux)
if: ${{ runner.os == 'Linux' && matrix.java == '17' }} # Only upload artifacts built from latest java on one OS
if: ${{ runner.os == 'Linux' && matrix.java == '21' }} # Only upload artifacts built from latest java on one OS
uses: actions/upload-artifact@v2
with:
name: Artifacts
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/de/dafuqs/revelationary/ClientAdvancements.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

@Environment(EnvType.CLIENT)
public class ClientAdvancements {

protected static boolean receivedFirstAdvancementPacket = false;
public static List<ClientAdvancementPacketCallback> callbacks = new ArrayList<>();

Expand Down Expand Up @@ -77,5 +76,4 @@ public static boolean hasDone(Identifier identifier) {
public static void playerLogout() {
receivedFirstAdvancementPacket = false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static void processRemovedAdvancements(@NotNull Set<Identifier> removedAd
// rerender chunks to show newly swapped blocks
static void rebuildAllChunks() {
WorldRenderer renderer = MinecraftClient.getInstance().worldRenderer;
((WorldRendererAccessor) renderer).rebuildAllChunks();
((WorldRendererAccessor) renderer).revelationary$rebuildAllChunks();
}

// BLOCKS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Map;

public class RevelationDataLoader extends JsonDataLoader implements IdentifiableResourceReloadListener {

public static final RevelationDataLoader INSTANCE = new RevelationDataLoader();

private RevelationDataLoader() {
Expand All @@ -21,11 +20,11 @@ private RevelationDataLoader() {
@Override
protected void apply(Map<Identifier, JsonElement> prepared, ResourceManager manager, Profiler profiler) {
prepared.forEach((identifier, jsonElement) -> RevelationRegistry.registerFromJson(jsonElement.getAsJsonObject()));
RevelationRegistry.deepTrim();
}

@Override
public Identifier getFabricId() {
return new Identifier(Revelationary.MOD_ID, "revelations");
}

}
376 changes: 171 additions & 205 deletions src/main/java/de/dafuqs/revelationary/RevelationRegistry.java

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions src/main/java/de/dafuqs/revelationary/Revelationary.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package de.dafuqs.revelationary;

import de.dafuqs.revelationary.api.advancements.*;
import de.dafuqs.revelationary.networking.RevelationaryPackets;
import net.fabricmc.api.*;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.*;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.resource.*;
import net.fabricmc.loader.api.*;
import net.minecraft.resource.*;
import org.slf4j.*;

public class Revelationary implements ModInitializer {

public static final String MOD_ID = "revelationary";
private static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);

Expand All @@ -27,24 +24,28 @@ public static void logWarning(String message) {
public static void logError(String message) {
LOGGER.error("[Revelationary] {}", message);
}
public static void logException(Throwable t) {
LOGGER.error("[Revelationary] ", t);
}

@Override
public void onInitialize() {
logInfo("Starting Common Startup");

PayloadTypeRegistry.playS2C().register(RevelationaryPackets.RevelationSync.ID, RevelationaryPackets.RevelationSync.CODEC);
RevelationaryNetworking.register();

AdvancementCriteria.register();
CommandRegistrationCallback.EVENT.register(Commands::register);
ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(RevelationDataLoader.INSTANCE);

ServerLifecycleEvents.SERVER_STARTED.register(server -> RevelationRegistry.addRevelationAwares());

ServerLifecycleEvents.SERVER_STARTED.register(server -> {
RevelationRegistry.addRevelationAwares();
RevelationRegistry.deepTrim();
});
if (FabricLoader.getInstance().isModLoaded("sodium")) {
logWarning("Sodium detected. Chunk rebuilding will be done in cursed mode.");
}

logInfo("Common startup completed!");
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package de.dafuqs.revelationary;

import de.dafuqs.revelationary.networking.RevelationaryS2CPacketReceivers;
import net.fabricmc.api.ClientModInitializer;

public class RevelationaryClient implements ClientModInitializer {

@Override
public void onInitializeClient() {
RevelationaryS2CPacketReceivers.register();
RevelationaryNetworking.registerPacketReceivers();
}

}
197 changes: 197 additions & 0 deletions src/main/java/de/dafuqs/revelationary/RevelationaryNetworking.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package de.dafuqs.revelationary;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.command.argument.BlockArgumentParser;
import net.minecraft.item.Item;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.registry.Registries;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.TextCodecs;
import net.minecraft.util.Identifier;

import java.util.Map;

public class RevelationaryNetworking {
public static void register() {
PayloadTypeRegistry.playS2C().register(RevelationSync.ID, RevelationSync.CODEC);
}

@Environment(EnvType.CLIENT)
public static void registerPacketReceivers() {
ClientPlayNetworking.registerGlobalReceiver(RevelationaryNetworking.RevelationSync.ID, (payload, context) -> {
try {
RevelationRegistry.fromPacket(payload);
} catch (Exception e) {
Revelationary.logError("Error fetching results from sync packet");
Revelationary.logException(e);
}
ClientRevelationHolder.cloakAll();
});
}

public static void sendRevelations(ServerPlayerEntity player) {
ServerPlayNetworking.send(player, RevelationRegistry.intoPacket());
}

public record RevelationSync(Object2ObjectOpenHashMap<Identifier, ObjectArrayList<BlockState>> advToBlockStates,
Object2ObjectOpenHashMap<BlockState, Identifier> blockStateToAdv,
Object2ObjectOpenHashMap<BlockState, BlockState> blockStateCloaks,
Object2ObjectOpenHashMap<Block, Block> blockCloaks,
Object2ObjectOpenHashMap<Identifier, ObjectArrayList<Item>> advToItems,
Object2ObjectOpenHashMap<Item, Identifier> itemToAdv,
Object2ObjectOpenHashMap<Item, Item> itemCloaks,
Object2ObjectOpenHashMap<Block, MutableText> cloakedBlockNameTranslations,
Object2ObjectOpenHashMap<Item, MutableText> cloakedItemNameTranslations) implements CustomPayload {
public static final PacketCodec<RegistryByteBuf, RevelationSync> CODEC = CustomPayload.codecOf(RevelationSync::write, RevelationSync::read);
public static final CustomPayload.Id<RevelationSync> ID = new Id<>(new Identifier(Revelationary.MOD_ID, "revelation_sync"));

private static void writeText(RegistryByteBuf buf, Text text) {
TextCodecs.REGISTRY_PACKET_CODEC.encode(buf, text);
}

private static Text readText(RegistryByteBuf buf) {
return TextCodecs.REGISTRY_PACKET_CODEC.decode(buf);
}

public static RevelationSync read(RegistryByteBuf buf) {
/* Block States */

final Object2ObjectOpenHashMap<Block, Block> blockCloaks = new Object2ObjectOpenHashMap<>(buf.readInt());
final Object2ObjectOpenHashMap<BlockState, Identifier> blockStateToAdv = new Object2ObjectOpenHashMap<>(buf.readInt());
final Object2ObjectOpenHashMap<BlockState, BlockState> blockStateCloaks = new Object2ObjectOpenHashMap<>(buf.readInt());
int blockEntries = buf.readInt();
final Object2ObjectOpenHashMap<Identifier, ObjectArrayList<BlockState>> advToBlockStates = new Object2ObjectOpenHashMap<>(blockEntries);
for (int i = 0; i < blockEntries; i++) {
Identifier advancementIdentifier = buf.readIdentifier();
int blockStateCount = buf.readInt();
ObjectArrayList<BlockState> advancementStates = new ObjectArrayList<>(blockStateCount);
for (int j = 0; j < blockStateCount; j++) {
try {
BlockState sourceState = BlockArgumentParser.block(Registries.BLOCK.getReadOnlyWrapper(), buf.readString(), true).blockState();
BlockState targetState = BlockArgumentParser.block(Registries.BLOCK.getReadOnlyWrapper(), buf.readString(), true).blockState();

advancementStates.add(sourceState);
blockStateToAdv.put(sourceState, advancementIdentifier);
blockStateCloaks.put(sourceState, targetState);
blockCloaks.putIfAbsent(sourceState.getBlock(), targetState.getBlock());
} catch (CommandSyntaxException e) {
Revelationary.logError(e.getMessage());
}
}
advToBlockStates.put(advancementIdentifier, advancementStates);
}

/* Items */

final Object2ObjectOpenHashMap<Item, Identifier> itemToAdv = new Object2ObjectOpenHashMap<>(buf.readInt());
Object2ObjectOpenHashMap<Item, Item> itemCloaks = new Object2ObjectOpenHashMap<>(buf.readInt());
int itemEntries = buf.readInt();
final Object2ObjectOpenHashMap<Identifier, ObjectArrayList<Item>> advToItems = new Object2ObjectOpenHashMap<>(itemEntries); // preallocate this map too
for (int i = 0; i < itemEntries; i++) {
Identifier advancementIdentifier = buf.readIdentifier();
int itemCount = buf.readInt();
ObjectArrayList<Item> advancementItems = new ObjectArrayList<>(itemCount);
for (int j = 0; j < itemCount; j++) {
Identifier sourceId = Identifier.tryParse(buf.readString());
Identifier targetId = Identifier.tryParse(buf.readString());
Item sourceItem = Registries.ITEM.get(sourceId);
Item targetItem = Registries.ITEM.get(targetId);

advancementItems.add(sourceItem);
itemToAdv.put(sourceItem, advancementIdentifier);
itemCloaks.put(sourceItem, targetItem);
}
advToItems.put(advancementIdentifier, advancementItems);
}

/* Block Translations */
int blockTranslations = buf.readInt();
final Object2ObjectOpenHashMap<Block, MutableText> cloakedBlockNameTranslations = new Object2ObjectOpenHashMap<>(blockTranslations); // preallocate translations
for (int i = 0; i < blockTranslations; i++) {
Block block = Registries.BLOCK.get(buf.readIdentifier());
MutableText text = (MutableText) readText(buf);
cloakedBlockNameTranslations.put(block, text);
}

/* Item Translations */
int itemTranslations = buf.readInt();
final Object2ObjectOpenHashMap<Item, MutableText> cloakedItemNameTranslations = new Object2ObjectOpenHashMap<>(itemTranslations); // preallocate translations
for (int i = 0; i < itemTranslations; i++) {
Item item = Registries.ITEM.get(buf.readIdentifier());
MutableText text = (MutableText) readText(buf);
cloakedItemNameTranslations.put(item, text);
}
return new RevelationSync(advToBlockStates,
blockStateToAdv,
blockStateCloaks,
blockCloaks,
advToItems,
itemToAdv,
itemCloaks,
cloakedBlockNameTranslations,
cloakedItemNameTranslations);
}

public void write(RegistryByteBuf buf) {
// Block States
buf.writeInt(blockCloaks.size()); // for preallocation on packet read
buf.writeInt(blockStateToAdv.size()); // for preallocation on packet read
buf.writeInt(blockStateCloaks.size()); // for preallocation on packet read
buf.writeInt(advToBlockStates.size());
for (Map.Entry<Identifier, ObjectArrayList<BlockState>> advancementBlocks : advToBlockStates.entrySet()) {
buf.writeIdentifier(advancementBlocks.getKey());
buf.writeInt(advancementBlocks.getValue().size());
for (BlockState blockState : advancementBlocks.getValue()) {
buf.writeString(BlockArgumentParser.stringifyBlockState(blockState));
buf.writeString(BlockArgumentParser.stringifyBlockState(blockStateCloaks.get(blockState)));
}
}

// Items
buf.writeInt(itemToAdv.size()); // for preallocation on packet read
buf.writeInt(itemCloaks.size()); // for preallocation on packet read
buf.writeInt(advToItems.size());
for (Map.Entry<Identifier, ObjectArrayList<Item>> advancementItems : advToItems.entrySet()) {
buf.writeIdentifier(advancementItems.getKey());
buf.writeInt(advancementItems.getValue().size());
for (Item item : advancementItems.getValue()) {
buf.writeString(Registries.ITEM.getId(item).toString());
buf.writeString(Registries.ITEM.getId(itemCloaks.get(item)).toString());
}
}

// Block Translations
buf.writeInt(cloakedBlockNameTranslations.size());
for (Map.Entry<Block, MutableText> blockTranslation : cloakedBlockNameTranslations.entrySet()) {
buf.writeIdentifier(Registries.BLOCK.getId(blockTranslation.getKey()));
writeText(buf, blockTranslation.getValue());
}

// Item Translations
buf.writeInt(cloakedItemNameTranslations.size());
for (Map.Entry<Item, MutableText> itemTranslation : cloakedItemNameTranslations.entrySet()) {
buf.writeIdentifier(Registries.ITEM.getId(itemTranslation.getKey()));
writeText(buf, itemTranslation.getValue());
}
}

@Override
public Id<RevelationSync> getId() {
return ID;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.*;

public class AdvancementCountCriterion extends AbstractCriterion<AdvancementCountCriterion.Conditions> {

public void trigger(ServerPlayerEntity player) {
this.trigger(player, (conditions) -> conditions.matches(player));
}
Expand Down Expand Up @@ -55,5 +54,4 @@ public boolean matches(ServerPlayerEntity serverPlayerEntity) {
return this.range == null ? allMatched : this.range.test(matchingAdvancements);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.Optional;

public class AdvancementGottenCriterion extends AbstractCriterion<AdvancementGottenCriterion.Conditions> {

public void trigger(ServerPlayerEntity player, AdvancementEntry advancement) {
this.trigger(player, (conditions) -> conditions.matches(advancement));
}
Expand All @@ -35,5 +34,4 @@ public Identifier getAdvancementIdentifier() {
return advancementIdentifier;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Optional;

public class HadRevelationCriterion extends AbstractCriterion<HadRevelationCriterion.Conditions> {

public void trigger(ServerPlayerEntity player, Block block) {
this.trigger(player, (conditions) -> conditions.matches(block));
}
Expand Down Expand Up @@ -42,5 +41,4 @@ public boolean matches(Object object) {
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.minecraft.advancement.criterion.Criteria;

public class AdvancementCriteria {

/**
* Triggered every time a player gets a new advancement
*/
Expand All @@ -24,5 +23,4 @@ public static void register() {
HAD_REVELATION = Criteria.register("revelationary:had_revelation", new HadRevelationCriterion());
ADVANCEMENT_GOTTEN = Criteria.register("revelationary:advancement_gotten", new AdvancementGottenCriterion());
}

}
Loading
Loading