Skip to content

Commit

Permalink
Merge pull request #1231 from P3pp3rF1y/1.20.x-dev
Browse files Browse the repository at this point in the history
feat: ✨ Add logic to fill backpack that was dropped by mobs with loot…
  • Loading branch information
P3pp3rF1y authored Nov 22, 2024
2 parents 15c698b + 6447ac2 commit 5d76fb8
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 @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedbackpacks
mod_group_id=sophisticatedbackpacks
mod_version=3.20.16
mod_version=3.20.17
sonar_project_key=sophisticatedbackpacks:SophisticatedBackpacks
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedBackpacks

Expand Down Expand Up @@ -31,5 +31,5 @@ crafting_tweaks_cf_file_id=4596466
chipped_cf_file_id=5077656
resourcefullib_cf_file_id=5070629
athena_cf_file_id=4764357
sc_version=[1.20.1-0.6.17,1.20.4)
sc_version=[1.20.1-0.7.8,1.20.4)
parchment_version=2023.09.03-1.20.1
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.nbt.CompoundTag;
import net.minecraft.nbt.FloatTag;
import net.minecraft.nbt.IntTag;
Expand All @@ -10,8 +11,11 @@
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.minecraftforge.energy.IEnergyStorage;
import net.minecraftforge.fml.util.thread.SidedThreadGroups;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.server.ServerLifecycleHooks;
import net.p3pp3rf1y.sophisticatedbackpacks.SophisticatedBackpacks;
import net.p3pp3rf1y.sophisticatedbackpacks.api.CapabilityBackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.api.IEnergyStorageUpgradeWrapper;
Expand Down Expand Up @@ -156,6 +160,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 @@ -423,10 +430,16 @@ public void setLoot(ResourceLocation lootTableName, float lootPercentage) {

@Override
public void fillWithLoot(Player playerEntity) {
if (playerEntity.level().isClientSide) {
Level level = playerEntity.level();
if (level.isClientSide) {
return;
}
NBTHelper.getString(backpack, LOOT_TABLE_NAME_TAG).ifPresent(ltName -> fillWithLootFromTable(playerEntity, ltName));
BlockPos pos = playerEntity.blockPosition();
fillWithLoot(level, pos);
}

private void fillWithLoot(Level level, BlockPos pos) {
NBTHelper.getString(backpack, LOOT_TABLE_NAME_TAG).ifPresent(ltName -> fillWithLootFromTable(level, pos, ltName));
}

@Override
Expand Down Expand Up @@ -477,9 +490,9 @@ public int getColumnsTaken() {
return NBTHelper.getInt(backpack, COLUMNS_TAKEN_TAG).orElse(0);
}

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

Expand All @@ -489,7 +502,7 @@ private void fillWithLootFromTable(Player playerEntity, String lootName) {
backpack.removeTagKey(LOOT_TABLE_NAME_TAG);
backpack.removeTagKey(LOOT_PERCENTAGE_TAG);

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

0 comments on commit 5d76fb8

Please sign in to comment.