Skip to content

Commit

Permalink
🐛 Fixed invisible backpack bug on server
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiviacz1337 committed Dec 28, 2023
1 parent 08e6746 commit 7fef2d3
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 76 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ public static void equipBackpack(PlayerEntity player)

//Sync
ComponentUtils.sync(player);
ComponentUtils.syncToTracking(player);
}
((ServerPlayerEntity)player).closeHandledScreen();
}
Expand Down Expand Up @@ -173,7 +172,6 @@ public static void unequipBackpack(PlayerEntity player)

//Sync
ComponentUtils.sync(player);
ComponentUtils.syncToTracking(player);
}
((ServerPlayerEntity)player).closeHandledScreen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public void addToGrave(ServerPlayerEntity serverPlayerEntity, ItemConsumer itemC

//Sync
ComponentUtils.sync(serverPlayerEntity);
ComponentUtils.syncToTracking(serverPlayerEntity);
}
}
}
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -100,7 +87,6 @@ public static void equipBackpack(PlayerEntity player, ItemStack stack)
}

sync(player);
syncToTracking(player);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Component>, AutoSyncedComponent
{
Expand All @@ -22,6 +21,4 @@ public interface ITravelersBackpackComponent extends PlayerComponent<Component>,
void setContents(ItemStack stack);

void sync();

void syncToTracking(ServerPlayerEntity player);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public static void registerListeners()
ComponentUtils.getComponent(player).removeWearable();

ComponentUtils.sync(player);
ComponentUtils.syncToTracking(player);

return ActionResult.SUCCESS;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/tiviacz/travelersbackpack/init/ModNetwork.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
{
Expand All @@ -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) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ public void sendPackets()
if(screenID == Reference.WEARABLE_SCREEN_ID)
{
ComponentUtils.sync(this.player);
ComponentUtils.syncToTracking(player);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit 7fef2d3

Please sign in to comment.