diff --git a/gradle.properties b/gradle.properties index 4c62d4c2..0e524bfb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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. diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackItem.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackItem.java index f502ee08..40c5d202 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackItem.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackItem.java @@ -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; } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackStorage.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackStorage.java index 0bce4af8..0aa5fc5b 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackStorage.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/BackpackStorage.java @@ -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; @@ -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(); + } + } } diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/wrapper/BackpackWrapper.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/wrapper/BackpackWrapper.java index e49a0439..2ff6c86f 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/wrapper/BackpackWrapper.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/backpack/wrapper/BackpackWrapper.java @@ -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; @@ -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; @@ -81,6 +83,7 @@ public class BackpackWrapper implements IBackpackWrapper { }; private Runnable upgradeCachesInvalidatedHandler = () -> { }; + public BackpackWrapper(ItemStack backpackStack) { setBackpackStack(backpackStack); } @@ -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) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/ClientEventHandler.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/ClientEventHandler.java index 291b8282..2e259cd3 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/ClientEventHandler.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/client/ClientEventHandler.java @@ -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.*; @@ -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) { diff --git a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/common/EntityBackpackAdditionHandler.java b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/common/EntityBackpackAdditionHandler.java index 688a4c5f..6ce023ea 100644 --- a/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/common/EntityBackpackAdditionHandler.java +++ b/src/main/java/net/p3pp3rf1y/sophisticatedbackpacks/common/EntityBackpackAdditionHandler.java @@ -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; @@ -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; @@ -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; @@ -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); @@ -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 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;