diff --git a/build.gradle b/build.gradle index 8be79288e..fd51328c8 100644 --- a/build.gradle +++ b/build.gradle @@ -113,7 +113,7 @@ dependencies { modImplementation "me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}" //include "me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}" - //modImplementation("com.terraformersmc:modmenu:${project.mod_menu_version}") + modRuntimeOnly("com.terraformersmc:modmenu:${project.mod_menu_version}") modImplementation "dev.emi:trinkets:${trinkets_version}" diff --git a/gradle.properties b/gradle.properties index 9e7e8eb73..c040af7de 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G loader_version=0.14.22 # Mod Properties - mod_version = 1.20.1-9.1.8 + mod_version = 1.20.1-9.1.9 maven_group = com.tiviacz.travelersbackpack archives_base_name = travelers-backpack @@ -19,6 +19,6 @@ org.gradle.jvmargs=-Xmx1G emi_version = 1.0.5+1.20.1 cloth_config_version = 11.1.106 rei_version = 12.0.645 - #mod_menu_version = 6.1.0 + mod_menu_version = 7.1.0 architectury_version = 9.1.12 #reach_entity_attributes_version = 2.3.2 diff --git a/src/main/java/com/tiviacz/travelersbackpack/TravelersBackpackClient.java b/src/main/java/com/tiviacz/travelersbackpack/TravelersBackpackClient.java index 9988fbd9c..9744c4d96 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/TravelersBackpackClient.java +++ b/src/main/java/com/tiviacz/travelersbackpack/TravelersBackpackClient.java @@ -7,6 +7,7 @@ import com.tiviacz.travelersbackpack.client.screen.TravelersBackpackHandledScreen; import com.tiviacz.travelersbackpack.client.screen.tooltip.BackpackTooltipComponent; import com.tiviacz.travelersbackpack.client.screen.tooltip.BackpackTooltipData; +import com.tiviacz.travelersbackpack.component.ComponentUtils; import com.tiviacz.travelersbackpack.fluids.milk.MilkFluidVariantAttributeHandler; import com.tiviacz.travelersbackpack.fluids.potion.PotionFluidVariantAttributeHandler; import com.tiviacz.travelersbackpack.fluids.potion.PotionFluidVariantRenderHandler; @@ -17,6 +18,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap; +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.client.render.fluid.v1.FluidRenderHandlerRegistry; import net.fabricmc.fabric.api.client.render.fluid.v1.SimpleFluidRenderHandler; import net.fabricmc.fabric.api.client.rendering.v1.BlockEntityRendererRegistry; @@ -32,8 +34,11 @@ import net.minecraft.client.render.entity.LivingEntityRenderer; import net.minecraft.client.render.entity.PlayerEntityRenderer; import net.minecraft.client.render.entity.model.BipedEntityModel; +import net.minecraft.entity.Entity; import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.util.Identifier; @@ -65,10 +70,34 @@ public void onInitializeClient() KeybindHandler.initKeybinds(); KeybindHandler.registerListeners(); ModNetwork.initClient(); + initClientNetworkMessage(); registerModelPredicate(); setupFluidRendering(); } + public static void initClientNetworkMessage() + { + ClientPlayNetworking.registerGlobalReceiver(ModNetwork.SYNC_BACKPACK_ID, (client, handler, buf, sender) -> + { + int entityId = buf.readInt(); + NbtCompound compound = buf.readNbt(); + + client.execute(() -> + { + if(client.world != null) + { + Entity entity = client.world.getEntityById(entityId); + + if(entity instanceof PlayerEntity player) + { + ComponentUtils.getComponent(player).setWearable(ItemStack.fromNbt(compound)); + ComponentUtils.getComponent(player).setContents(ItemStack.fromNbt(compound)); + } + } + }); + }); + } + public static void setupFluidRendering() { FluidRenderHandlerRegistry.INSTANCE.register(ModFluids.POTION_STILL, ModFluids.POTION_FLOWING, new SimpleFluidRenderHandler( diff --git a/src/main/java/com/tiviacz/travelersbackpack/commands/ClearBackpackCommand.java b/src/main/java/com/tiviacz/travelersbackpack/commands/ClearBackpackCommand.java index 6bbea8d5b..68a644a52 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/commands/ClearBackpackCommand.java +++ b/src/main/java/com/tiviacz/travelersbackpack/commands/ClearBackpackCommand.java @@ -43,7 +43,6 @@ private static int removeBackpack(ServerCommandSource source, ServerPlayerEntity component.setContents(ItemStack.EMPTY); component.sync(); - component.syncToTracking(player); source.sendFeedback(() -> Text.literal("Removed Traveler's Backpack from " + player.getDisplayName().getString()), true); return 1; @@ -68,7 +67,6 @@ private static int clearBackpack(ServerCommandSource source, ServerPlayerEntity component.setContents(ItemStack.EMPTY); component.sync(); - component.syncToTracking(player); source.sendFeedback(() -> Text.literal("Cleared contents of Traveler's Backpack from " + player.getDisplayName().getString()), true); return 1; diff --git a/src/main/java/com/tiviacz/travelersbackpack/common/ServerActions.java b/src/main/java/com/tiviacz/travelersbackpack/common/ServerActions.java index 12d8583ca..dbfbb4ea1 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/common/ServerActions.java +++ b/src/main/java/com/tiviacz/travelersbackpack/common/ServerActions.java @@ -142,7 +142,6 @@ public static void equipBackpack(PlayerEntity player) //Sync ComponentUtils.sync(player); - ComponentUtils.syncToTracking(player); } ((ServerPlayerEntity)player).closeHandledScreen(); } @@ -173,7 +172,6 @@ public static void unequipBackpack(PlayerEntity player) //Sync ComponentUtils.sync(player); - ComponentUtils.syncToTracking(player); } ((ServerPlayerEntity)player).closeHandledScreen(); } diff --git a/src/main/java/com/tiviacz/travelersbackpack/compat/universalgraves/UniversalGravesCompat.java b/src/main/java/com/tiviacz/travelersbackpack/compat/universalgraves/UniversalGravesCompat.java index 6e75e7213..2e90b0014 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/compat/universalgraves/UniversalGravesCompat.java +++ b/src/main/java/com/tiviacz/travelersbackpack/compat/universalgraves/UniversalGravesCompat.java @@ -35,7 +35,6 @@ public void addToGrave(ServerPlayerEntity serverPlayerEntity, ItemConsumer itemC //Sync ComponentUtils.sync(serverPlayerEntity); - ComponentUtils.syncToTracking(serverPlayerEntity); } } } @@ -51,7 +50,6 @@ public boolean moveToPlayerExactly(ServerPlayerEntity serverPlayerEntity, ItemSt //Sync ComponentUtils.sync(serverPlayerEntity); - ComponentUtils.syncToTracking(serverPlayerEntity); serverPlayerEntity.getWorld().playSound(null, serverPlayerEntity.getBlockPos(), SoundEvents.ITEM_ARMOR_EQUIP_LEATHER, SoundCategory.PLAYERS, 1.0F, (1.0F + (serverPlayerEntity.getWorld().random.nextFloat() - serverPlayerEntity.getWorld().random.nextFloat()) * 0.2F) * 0.7F); diff --git a/src/main/java/com/tiviacz/travelersbackpack/component/ComponentUtils.java b/src/main/java/com/tiviacz/travelersbackpack/component/ComponentUtils.java index b5782869e..9e2988844 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/component/ComponentUtils.java +++ b/src/main/java/com/tiviacz/travelersbackpack/component/ComponentUtils.java @@ -45,19 +45,6 @@ public static void sync(PlayerEntity player) } } - public static void syncToTracking(PlayerEntity player) - { - if(player instanceof ServerPlayerEntity serverPlayer) - { - getComponent(player).syncToTracking(serverPlayer); - } - } - - public static void synchroniseEntity(LivingEntity livingEntity) - { - getComponent(livingEntity).sync(); - } - public static boolean isWearingBackpack(PlayerEntity player) { if(TravelersBackpack.enableTrinkets()) @@ -100,7 +87,6 @@ public static void equipBackpack(PlayerEntity player, ItemStack stack) } sync(player); - syncToTracking(player); } @Nullable diff --git a/src/main/java/com/tiviacz/travelersbackpack/component/ITravelersBackpackComponent.java b/src/main/java/com/tiviacz/travelersbackpack/component/ITravelersBackpackComponent.java index 46f10b596..ee3950b87 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/component/ITravelersBackpackComponent.java +++ b/src/main/java/com/tiviacz/travelersbackpack/component/ITravelersBackpackComponent.java @@ -5,7 +5,6 @@ import dev.onyxstudios.cca.api.v3.component.sync.AutoSyncedComponent; import dev.onyxstudios.cca.api.v3.entity.PlayerComponent; import net.minecraft.item.ItemStack; -import net.minecraft.server.network.ServerPlayerEntity; public interface ITravelersBackpackComponent extends PlayerComponent, AutoSyncedComponent { @@ -22,6 +21,4 @@ public interface ITravelersBackpackComponent extends PlayerComponent, void setContents(ItemStack stack); void sync(); - - void syncToTracking(ServerPlayerEntity player); } \ No newline at end of file diff --git a/src/main/java/com/tiviacz/travelersbackpack/component/TravelersBackpackComponent.java b/src/main/java/com/tiviacz/travelersbackpack/component/TravelersBackpackComponent.java index 90c1d40d1..e8efcc793 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/component/TravelersBackpackComponent.java +++ b/src/main/java/com/tiviacz/travelersbackpack/component/TravelersBackpackComponent.java @@ -2,15 +2,9 @@ import com.tiviacz.travelersbackpack.inventory.TravelersBackpackInventory; import com.tiviacz.travelersbackpack.util.Reference; -import dev.onyxstudios.cca.internal.entity.CardinalComponentsEntity; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.PlayerLookup; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.network.ServerPlayerEntity; public class TravelersBackpackComponent implements ITravelersBackpackComponent { @@ -76,27 +70,7 @@ public void setContents(ItemStack stack) @Override public void sync() { - syncWith((ServerPlayerEntity)this.player); - } - - public void syncToTracking(ServerPlayerEntity player) - { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeInt(player.getId()); - buf.writeIdentifier(ComponentUtils.WEARABLE.getId()); - this.writeSyncPacket(buf, player); - for(ServerPlayerEntity serverPlayer : PlayerLookup.tracking(player)) - { - ServerPlayNetworking.send(serverPlayer, CardinalComponentsEntity.PACKET_ID, buf); - } - } - - public void syncWith(ServerPlayerEntity player) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeInt(player.getId()); - buf.writeIdentifier(ComponentUtils.WEARABLE.getId()); - this.writeSyncPacket(buf, player); - ServerPlayNetworking.send(player, CardinalComponentsEntity.PACKET_ID, buf); + ComponentUtils.WEARABLE.sync(this.player); } @Override diff --git a/src/main/java/com/tiviacz/travelersbackpack/component/entity/EntityTravelersBackpackComponent.java b/src/main/java/com/tiviacz/travelersbackpack/component/entity/EntityTravelersBackpackComponent.java index e95148091..3a2916bb8 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/component/entity/EntityTravelersBackpackComponent.java +++ b/src/main/java/com/tiviacz/travelersbackpack/component/entity/EntityTravelersBackpackComponent.java @@ -1,15 +1,9 @@ package com.tiviacz.travelersbackpack.component.entity; import com.tiviacz.travelersbackpack.component.ComponentUtils; -import dev.onyxstudios.cca.internal.entity.CardinalComponentsEntity; -import io.netty.buffer.Unpooled; -import net.fabricmc.fabric.api.networking.v1.PlayerLookup; -import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.entity.LivingEntity; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; -import net.minecraft.network.PacketByteBuf; -import net.minecraft.server.network.ServerPlayerEntity; public class EntityTravelersBackpackComponent implements IEntityTravelersBackpackComponent { @@ -48,20 +42,7 @@ public void removeWearable() @Override public void sync() { - syncToTracking(); - } - - public void syncToTracking() - { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeInt(this.livingEntity.getId()); - buf.writeIdentifier(ComponentUtils.ENTITY_WEARABLE.getId()); - - for(ServerPlayerEntity serverPlayer : PlayerLookup.tracking(this.livingEntity)) - { - this.writeSyncPacket(buf, serverPlayer); - ServerPlayNetworking.send(serverPlayer, CardinalComponentsEntity.PACKET_ID, buf); - } + ComponentUtils.ENTITY_WEARABLE.sync(livingEntity); } @Override diff --git a/src/main/java/com/tiviacz/travelersbackpack/handlers/RightClickHandler.java b/src/main/java/com/tiviacz/travelersbackpack/handlers/RightClickHandler.java index e5a2b604c..7f4775438 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/handlers/RightClickHandler.java +++ b/src/main/java/com/tiviacz/travelersbackpack/handlers/RightClickHandler.java @@ -62,7 +62,6 @@ public static void registerListeners() ComponentUtils.getComponent(player).removeWearable(); ComponentUtils.sync(player); - ComponentUtils.syncToTracking(player); return ActionResult.SUCCESS; } diff --git a/src/main/java/com/tiviacz/travelersbackpack/init/ModNetwork.java b/src/main/java/com/tiviacz/travelersbackpack/init/ModNetwork.java index ba703357d..9cc25f635 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/init/ModNetwork.java +++ b/src/main/java/com/tiviacz/travelersbackpack/init/ModNetwork.java @@ -12,11 +12,13 @@ import com.tiviacz.travelersbackpack.util.Reference; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; +import net.fabricmc.fabric.api.networking.v1.PlayerLookup; import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents; import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.network.PacketByteBuf; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.BlockPos; @@ -32,6 +34,7 @@ public class ModNetwork public static final Identifier MEMORY_ID = new Identifier(TravelersBackpack.MODID, "memory"); public static final Identifier SETTINGS_ID = new Identifier(TravelersBackpack.MODID, "settings"); public static final Identifier UPDATE_CONFIG_ID = new Identifier(TravelersBackpack.MODID,"update_config"); + public static final Identifier SYNC_BACKPACK_ID = new Identifier(TravelersBackpack.MODID, "sync_backpack"); public static void initClient() { @@ -55,6 +58,24 @@ public static void initServer() PacketByteBuf buf = PacketByteBufs.create(); buf.writeNbt(TravelersBackpackConfig.toNbt()); sender.sendPacket(ModNetwork.UPDATE_CONFIG_ID, buf); + + //Packets to sync backpack component to client on login (Cardinal Components autosync somehow doesn't sync properly) + + //Sync to target client + PacketByteBuf buf2 = PacketByteBufs.create(); + buf2.writeInt(handler.getPlayer().getId()); + buf2.writeNbt(ComponentUtils.getWearingBackpack(handler.getPlayer()).writeNbt(new NbtCompound())); + sender.sendPacket(ModNetwork.SYNC_BACKPACK_ID, buf2); + + //Sync backpacks of all players in radius of 64 blocks + for(ServerPlayerEntity serverPlayer : PlayerLookup.around(handler.getPlayer().getServerWorld(), handler.getPlayer().getPos(), 64)) + { + PacketByteBuf buf3 = PacketByteBufs.create(); + buf3.writeInt(serverPlayer.getId()); + buf3.writeNbt(ComponentUtils.getWearingBackpack(serverPlayer).writeNbt(new NbtCompound())); + + sender.sendPacket(ModNetwork.SYNC_BACKPACK_ID, buf3); + } }); ServerPlayNetworking.registerGlobalReceiver(EQUIP_BACKPACK_ID, (server, player, handler, buf, response) -> diff --git a/src/main/java/com/tiviacz/travelersbackpack/inventory/TravelersBackpackInventory.java b/src/main/java/com/tiviacz/travelersbackpack/inventory/TravelersBackpackInventory.java index 1e94b2b50..38f8b1eb4 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/inventory/TravelersBackpackInventory.java +++ b/src/main/java/com/tiviacz/travelersbackpack/inventory/TravelersBackpackInventory.java @@ -253,7 +253,6 @@ public void sendPackets() if(screenID == Reference.WEARABLE_SCREEN_ID) { ComponentUtils.sync(this.player); - ComponentUtils.syncToTracking(player); } } diff --git a/src/main/java/com/tiviacz/travelersbackpack/inventory/screen/slot/ToolSlot.java b/src/main/java/com/tiviacz/travelersbackpack/inventory/screen/slot/ToolSlot.java index d3eb61887..8ec3abe06 100644 --- a/src/main/java/com/tiviacz/travelersbackpack/inventory/screen/slot/ToolSlot.java +++ b/src/main/java/com/tiviacz/travelersbackpack/inventory/screen/slot/ToolSlot.java @@ -60,7 +60,6 @@ public void markDirty() if(inventory.getScreenID() == Reference.WEARABLE_SCREEN_ID && !player.getWorld().isClient) { ComponentUtils.sync(this.player); - ComponentUtils.syncToTracking(this.player); } } } \ No newline at end of file