Skip to content

Commit

Permalink
Sync entity tiers from server to client
Browse files Browse the repository at this point in the history
  • Loading branch information
OffsetMonkey538 committed Oct 29, 2023
1 parent bf94581 commit d54571d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import top.offsetmonkey538.compactmobfarms.block.entity.ModBlockEntityTypes;
import top.offsetmonkey538.compactmobfarms.client.gui.screen.ingame.CompactMobFarmScreen;
import top.offsetmonkey538.compactmobfarms.client.render.block.entity.CompactMobFarmBlockEntityRenderer;
import top.offsetmonkey538.compactmobfarms.config.EntityTiers;
import top.offsetmonkey538.compactmobfarms.item.FilledSampleTakerItem;
import top.offsetmonkey538.compactmobfarms.item.ModItems;
import top.offsetmonkey538.compactmobfarms.item.SampleTakerItem;
Expand Down Expand Up @@ -117,5 +118,10 @@ public void onInitializeClient() {

screen.displayProblemMessage(problemMessage);
});

ClientPlayNetworking.registerGlobalReceiver(ModPackets.UPDATE_ENTITY_TIER_LIST, (client, handler, buf, responseSender) -> {
EntityTiers.INSTANCE.clear();
EntityTiers.INSTANCE.fromUpdatePacket(buf);
});
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package top.offsetmonkey538.compactmobfarms;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.loot.v2.LootTableEvents;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.fabricmc.fabric.api.transfer.v1.item.ItemStorage;
import net.minecraft.block.Blocks;
Expand All @@ -11,15 +14,18 @@
import net.minecraft.loot.function.ApplyBonusLootFunction;
import net.minecraft.loot.function.SetCountLootFunction;
import net.minecraft.loot.provider.number.UniformLootNumberProvider;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.resource.ResourceType;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.offsetmonkey538.compactmobfarms.block.ModBlocks;
import top.offsetmonkey538.compactmobfarms.block.entity.ModBlockEntityTypes;
import top.offsetmonkey538.compactmobfarms.config.EntityTierResourceReloadListener;
import top.offsetmonkey538.compactmobfarms.config.EntityTiers;
import top.offsetmonkey538.compactmobfarms.item.ModItems;
import top.offsetmonkey538.compactmobfarms.item.group.ModItemGroups;
import top.offsetmonkey538.compactmobfarms.network.ModPackets;
import top.offsetmonkey538.compactmobfarms.recipe.ModRecipes;
import top.offsetmonkey538.compactmobfarms.screen.ModScreenHandlers;

Expand Down Expand Up @@ -51,6 +57,14 @@ public void onInitialize() {
});

ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new EntityTierResourceReloadListener());

ServerLifecycleEvents.SYNC_DATA_PACK_CONTENTS.register((player, joined) -> {
final PacketByteBuf buf = PacketByteBufs.create();

EntityTiers.INSTANCE.toUpdatePacket(buf);

ServerPlayNetworking.send(player, ModPackets.UPDATE_ENTITY_TIER_LIST, buf);
});
}

public static Identifier id(String path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package top.offsetmonkey538.compactmobfarms.config;

import blue.endless.jankson.annotation.SerializedName;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.entity.EntityType;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;

public class EntityTiers {
public static final EntityTiers INSTANCE = new EntityTiers();

@SerializedName("IGNORE_ME_PLEASE")
public List<EntityType<?>> SUPPORTED = new ArrayList<>();

public List<EntityType<?>> UNSUPPORTED = new ArrayList<>();
public List<EntityType<?>> TIER_0 = new ArrayList<>();
public List<EntityType<?>> TIER_1 = new ArrayList<>();
Expand Down Expand Up @@ -60,6 +66,7 @@ public void addOverride(EntityTiers override) {
}

public void clear() {
SUPPORTED.clear();
UNSUPPORTED.clear();
TIER_0.clear();
TIER_1.clear();
Expand All @@ -77,6 +84,20 @@ private void removeFromAllTiers(EntityType<?> entity) {
TIER_4.remove(entity);
}

public List<EntityType<?>> getSupported() {
if (!SUPPORTED.isEmpty()) return SUPPORTED;

SUPPORTED = new ArrayList<>(TIER_0.size() + TIER_1.size() + TIER_2.size() + TIER_3.size() + TIER_4.size());

SUPPORTED.addAll(TIER_0);
SUPPORTED.addAll(TIER_1);
SUPPORTED.addAll(TIER_2);
SUPPORTED.addAll(TIER_3);
SUPPORTED.addAll(TIER_4);

return SUPPORTED;
}

public int requiredTierFor(EntityType<?> entity) {
if (!isSupported(entity)) return -1;
if (TIER_0.contains(entity)) return 0;
Expand Down Expand Up @@ -121,6 +142,24 @@ public boolean anySupports(EntityType<?> entity) {
return tier4Supports(entity);
}

public void fromUpdatePacket(PacketByteBuf buf) {
UNSUPPORTED.addAll(buf.readList(buf1 -> buf1.readRegistryValue(Registries.ENTITY_TYPE)));
TIER_0.addAll(buf.readList(buf1 -> buf1.readRegistryValue(Registries.ENTITY_TYPE)));
TIER_1.addAll(buf.readList(buf1 -> buf1.readRegistryValue(Registries.ENTITY_TYPE)));
TIER_2.addAll(buf.readList(buf1 -> buf1.readRegistryValue(Registries.ENTITY_TYPE)));
TIER_3.addAll(buf.readList(buf1 -> buf1.readRegistryValue(Registries.ENTITY_TYPE)));
TIER_4.addAll(buf.readList(buf1 -> buf1.readRegistryValue(Registries.ENTITY_TYPE)));
}

public void toUpdatePacket(PacketByteBuf buf) {
buf.writeCollection(UNSUPPORTED, (buf1, entity) -> buf1.writeRegistryValue(Registries.ENTITY_TYPE, entity));
buf.writeCollection(TIER_0, (buf1, entity) -> buf1.writeRegistryValue(Registries.ENTITY_TYPE, entity));
buf.writeCollection(TIER_1, (buf1, entity) -> buf1.writeRegistryValue(Registries.ENTITY_TYPE, entity));
buf.writeCollection(TIER_2, (buf1, entity) -> buf1.writeRegistryValue(Registries.ENTITY_TYPE, entity));
buf.writeCollection(TIER_3, (buf1, entity) -> buf1.writeRegistryValue(Registries.ENTITY_TYPE, entity));
buf.writeCollection(TIER_4, (buf1, entity) -> buf1.writeRegistryValue(Registries.ENTITY_TYPE, entity));
}

@Override
public String toString() {
return "\n" + "Unsupported: " + UNSUPPORTED +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ private ModPackets() {
public static final Identifier GUI_UPDATE_ATTACK_DAMAGE = id("gui_update_attack_damage");

public static final Identifier GUI_DISPLAY_PROBLEM_MESSAGE = id("gui_display_problem_message");

public static final Identifier UPDATE_ENTITY_TIER_LIST = id("update_entity_tier_list");
}

0 comments on commit d54571d

Please sign in to comment.