Skip to content

Commit

Permalink
feat: ✨ Add logic to fill backpack that was dropped by mobs with loot…
Browse files Browse the repository at this point in the history
… when hopper pipe start extracting from it without needing player to open it first
  • Loading branch information
P3pp3rF1y committed Nov 22, 2024
1 parent c86c72a commit 6f589f2
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions 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.20.25
mod_version=3.20.26
mod_group_id=sophisticatedbackpacks
mod_authors=P3pp3rF1y, Ridanisaurus
mod_description=Fancy and functional backpacks.
Expand All @@ -34,7 +34,7 @@ chipped_cf_file_id=5506938
resourcefullib_cf_file_id=5483169
athena_cf_file_id=5431579
curios_cf_file_id=5546342
sc_version=[1.21-0.6.41,1.22)
sc_version=[1.21-0.7.10,1.22)

#publish
curseforge_id=422301
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper;

import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
Expand All @@ -8,10 +9,13 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.neoforged.fml.util.thread.SidedThreadGroups;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.fluids.capability.IFluidHandler;
import net.neoforged.neoforge.fluids.capability.IFluidHandlerItem;
import net.neoforged.neoforge.server.ServerLifecycleHooks;
import net.p3pp3rf1y.sophisticatedbackpacks.SophisticatedBackpacks;
import net.p3pp3rf1y.sophisticatedbackpacks.api.IEnergyStorageUpgradeWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.api.IFluidHandlerWrapperUpgrade;
Expand Down Expand Up @@ -167,6 +171,9 @@ private void markBackpackContentsDirty() {
public ITrackedContentsItemHandler getInventoryForInputOutput() {
if (inventoryIOHandler == null) {
inventoryIOHandler = new InventoryIOHandler(this);
if (Thread.currentThread().getThreadGroup() == SidedThreadGroups.SERVER && ServerLifecycleHooks.getCurrentServer() != null) {
fillWithLoot(ServerLifecycleHooks.getCurrentServer().overworld(), BlockPos.ZERO);
}
}
return inventoryIOHandler.getFilteredItemHandler();
}
Expand Down Expand Up @@ -428,14 +435,20 @@ public void setLoot(ResourceLocation lootTableName, float lootFactor) {

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

private void fillWithLoot(Level level, BlockPos pos) {
ResourceLocation lootTable = getBackpackStack().get(ModDataComponents.LOOT_TABLE);
if (lootTable == null) {
return;
}
fillWithLootFromTable(playerEntity, lootTable);
fillWithLootFromTable(level, pos, lootTable);
}

@Override
Expand Down Expand Up @@ -496,9 +509,9 @@ public int getColumnsTaken() {
return getBackpackStack().getOrDefault(ModDataComponents.COLUMNS_TAKEN, 0);
}

private void fillWithLootFromTable(Player playerEntity, ResourceLocation lootTable) {
MinecraftServer server = playerEntity.level().getServer();
if (server == null || !(playerEntity.level() instanceof ServerLevel serverLevel)) {
private void fillWithLootFromTable(Level level, BlockPos pos, ResourceLocation lootTable) {
MinecraftServer server = level.getServer();
if (server == null || !(level instanceof ServerLevel serverLevel)) {
return;
}

Expand All @@ -507,7 +520,7 @@ private void fillWithLootFromTable(Player playerEntity, ResourceLocation lootTab
getBackpackStack().remove(ModDataComponents.LOOT_TABLE);
getBackpackStack().remove(ModDataComponents.LOOT_FACTOR);

List<ItemStack> loot = LootHelper.getLoot(lootTable, server, serverLevel, playerEntity);
List<ItemStack> loot = LootHelper.getLoot(lootTable, server, serverLevel, pos);
loot.removeIf(stack -> stack.getItem() instanceof BackpackItem);
loot = RandHelper.getNRandomElements(loot, (int) (loot.size() * lootFactor));
LootHelper.fillWithLoot(serverLevel.random, loot, getInventoryHandler());
Expand Down

0 comments on commit 6f589f2

Please sign in to comment.