Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Add logic to fill backpack that was dropped by mobs with loot… #1231

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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