Skip to content

Commit

Permalink
fix: 🐛 Fixed an issue where backpack contents would be created unnece…
Browse files Browse the repository at this point in the history
…ssarily on the client side and change the way backpacks with jukebox upgrade are dropped from mobs so that these also clean up backpack contents and keep the jukebox items on the stack itself until they are open by a player for the first time
  • Loading branch information
P3pp3rF1y committed Jan 13, 2025
1 parent 6507828 commit 5e63fdf
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ loader_version_range=[4,)
mod_id=sophisticatedbackpacks
mod_name=Sophisticated Backpacks
mod_license=GNU General Public License v3.0
mod_version=3.22.1
mod_version=3.22.2
mod_group_id=sophisticatedbackpacks
mod_authors=P3pp3rF1y, Ridanisaurus
mod_description=Fancy and functional backpacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,10 @@ public ItemStack stash(ItemStack storageStack, ItemStack stack, boolean simulate
@Override
public StashResult getItemStashable(HolderLookup.Provider registries, ItemStack storageStack, ItemStack stack) {
IBackpackWrapper wrapper = BackpackWrapper.fromStack(storageStack);
if (wrapper.getContentsUuid().isEmpty()) {
return StashResult.SPACE; //Assuming that backpack that has no contentsUuid is empty and will have inventory once contentsUuid is created and thus any item can be stashed into it
}

if (wrapper.getInventoryForUpgradeProcessing().insertItem(stack, true).getCount() == stack.getCount()) {
return StashResult.NO_SPACE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.storage.DimensionDataStorage;
import net.neoforged.fml.util.thread.SidedThreadGroups;
import net.neoforged.neoforge.event.level.LevelEvent;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import net.p3pp3rf1y.sophisticatedbackpacks.SophisticatedBackpacks;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.BackpackSettingsHandler;
Expand Down Expand Up @@ -150,4 +151,11 @@ public int removeNonPlayerBackpackContents(boolean onlyWithEmptyInventory) {
public boolean removeUpdatedBackpackSettingsFlag(UUID backpackUuid) {
return updatedBackpackSettingsFlags.remove(backpackUuid);
}

public static void onClientWorldLoad(LevelEvent.Load evt) {
if (evt.getLevel().isClientSide()) {
clientStorageCopy.backpackContents.clear();
clientStorageCopy.accessLogRecords.clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper;

import net.minecraft.core.BlockPos;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -9,6 +10,7 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.level.Level;
import net.neoforged.fml.util.thread.SidedThreadGroups;
import net.neoforged.neoforge.energy.IEnergyStorage;
Expand Down Expand Up @@ -81,6 +83,7 @@ public class BackpackWrapper implements IBackpackWrapper {
};
private Runnable upgradeCachesInvalidatedHandler = () -> {
};

public BackpackWrapper(ItemStack backpackStack) {
setBackpackStack(backpackStack);
}
Expand Down Expand Up @@ -434,13 +437,31 @@ public void setLoot(ResourceLocation lootTableName, float lootFactor) {
}

@Override
public void fillWithLoot(Player playerEntity) {
Level level = playerEntity.level();
public void fillWithLoot(Player player) {
Level level = player.level();
if (level.isClientSide) {
return;
}
BlockPos pos = playerEntity.blockPosition();
BlockPos pos = player.blockPosition();
fillWithLoot(level, pos);
fillWithExtraItems(player);
}

private void fillWithExtraItems(Player player) {
ItemStack backpack = getBackpackStack();
if (!backpack.has(DataComponents.CONTAINER)) {
return;
}

ItemContainerContents containerItems = backpack.getOrDefault(DataComponents.CONTAINER, ItemContainerContents.EMPTY);
for (int slot = 0; slot < containerItems.getSlots(); slot++) {
ItemStack stack = containerItems.getStackInSlot(slot);
if (stack.isEmpty()) {
continue;
}
InventoryHelper.insertOrDropItem(player, stack, getInventoryHandler());
}
backpack.remove(DataComponents.CONTAINER);
}

private void fillWithLoot(Level level, BlockPos pos) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import net.neoforged.neoforge.common.util.Lazy;
import net.neoforged.neoforge.network.PacketDistributor;
import net.p3pp3rf1y.sophisticatedbackpacks.SophisticatedBackpacks;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackStorage;
import net.p3pp3rf1y.sophisticatedbackpacks.client.init.ModBlockColors;
import net.p3pp3rf1y.sophisticatedbackpacks.client.init.ModItemColors;
import net.p3pp3rf1y.sophisticatedbackpacks.client.render.*;
Expand Down Expand Up @@ -57,6 +58,7 @@ public static void registerHandlers(IEventBus modBus) {
eventBus.addListener(ClientBackpackContentsTooltip::onWorldLoad);
eventBus.addListener(ClientEventHandler::handleBlockPick);
eventBus.addListener(ClientEventHandler::onPlayerLoggingIn);
eventBus.addListener(BackpackStorage::onClientWorldLoad);
}

private static void onPlayerLoggingIn(ClientPlayerNetworkEvent.LoggingIn event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.primitives.Ints;
import net.minecraft.core.Holder;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.server.MinecraftServer;
Expand All @@ -26,6 +27,7 @@
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.SpawnEggItem;
import net.minecraft.world.item.component.ItemContainerContents;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
Expand All @@ -41,8 +43,10 @@
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.IBackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.init.ModItems;
import net.p3pp3rf1y.sophisticatedcore.api.IStorageWrapper;
import net.p3pp3rf1y.sophisticatedcore.init.ModCoreDataComponents;
import net.p3pp3rf1y.sophisticatedcore.upgrades.jukebox.JukeboxUpgradeItem;
import net.p3pp3rf1y.sophisticatedcore.upgrades.jukebox.JukeboxUpgradeWrapper;
import net.p3pp3rf1y.sophisticatedcore.util.InventoryHelper;
import net.p3pp3rf1y.sophisticatedcore.util.RandHelper;
import net.p3pp3rf1y.sophisticatedcore.util.WeightedElement;

Expand Down Expand Up @@ -262,6 +266,8 @@ static void handleBackpackDrop(LivingDropsEvent event) {
ItemStack backpack = mob.getItemBySlot(EquipmentSlot.CHEST);
Config.Server.EntityBackpackAdditionsConfig additionsConfig = Config.SERVER.entityBackpackAdditions;
if (shouldDropBackpack(event, additionsConfig, mob, backpack)) {
putJukeboxItemsInContainerAndRemoveStorageUuid(event, backpack);

ItemEntity backpackEntity = new ItemEntity(mob.level(), mob.getX(), mob.getY(), mob.getZ(), backpack);
event.getDrops().add(backpackEntity);
mob.setItemSlot(EquipmentSlot.CHEST, ItemStack.EMPTY);
Expand All @@ -272,6 +278,30 @@ static void handleBackpackDrop(LivingDropsEvent event) {
}
}

private static void putJukeboxItemsInContainerAndRemoveStorageUuid(LivingDropsEvent event, ItemStack backpack) {
if (event.getEntity().getTags().remove(SPAWNED_WITH_JUKEBOX_UPGRADE)) {
List<ItemStack> inventoryItems = new ArrayList<>();
IBackpackWrapper backpackwrapper = BackpackWrapper.fromStack(backpack);
backpackwrapper.getUpgradeHandler().getTypeWrappers(JukeboxUpgradeItem.TYPE).forEach(wrapper -> {
InventoryHelper.iterate(wrapper.getDiscInventory(), (slot, stack) -> {
if (!stack.isEmpty()) {
inventoryItems.add(wrapper.getDiscInventory().extractItem(slot, stack.getCount(), false));
}
});
});
InventoryHelper.iterate(backpackwrapper.getUpgradeHandler(), (slot, stack) -> {
if (!stack.isEmpty()) {
inventoryItems.add(backpackwrapper.getUpgradeHandler().extractItem(slot, stack.getCount(), false));
}
});
UUID backpackUuid = backpack.remove(ModCoreDataComponents.STORAGE_UUID);
if (backpackUuid != null) {
BackpackStorage.get().removeBackpackContents(backpackUuid);
}
backpack.set(DataComponents.CONTAINER, ItemContainerContents.fromItems(inventoryItems));
}
}

private static boolean shouldDropBackpack(LivingDropsEvent event, Config.Server.EntityBackpackAdditionsConfig additionsConfig, LivingEntity mob, ItemStack backpack) {
if (!(event.getSource().getEntity() instanceof Player player)) {
return false;
Expand Down

0 comments on commit 5e63fdf

Please sign in to comment.