Skip to content

Commit

Permalink
Improved Beans Backpacks compat
Browse files Browse the repository at this point in the history
  • Loading branch information
B1n-ry committed Mar 10, 2024
1 parent 87a643a commit 0ec6d34
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
### Fixes
* Fixed mod clearing player inventory when trying to claim a grave that had generated
before removing a compatible inventory mod
* Improved compat and fixed issues from update of Beans Backpacks
* Improved compat and fixed crash with Beans Backpacks
* No longer sending error message to player when claiming the grave by breaking it

---
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ org.gradle.parallel=true
numismatic_version=568316:4996365
apoli_version=v2.9.0
levelz_version=517130:5024636
beans_backpacks_version=hT2LiZSl
beans_backpacks_version=H4JcgrtM

# Dev dependencies
cardinal_components_version=5.2.2
Expand Down
65 changes: 43 additions & 22 deletions src/main/java/com/b1n_ry/yigd/compat/BeansBackpacksCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import com.b1n_ry.yigd.data.DeathContext;
import com.b1n_ry.yigd.events.DropRuleEvent;
import com.b1n_ry.yigd.util.DropRule;
import com.beansgalaxy.backpacks.core.BackData;
import com.beansgalaxy.backpacks.platform.Services;
import com.beansgalaxy.backpacks.data.BackData;
import com.beansgalaxy.backpacks.platform.services.CompatHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.Pair;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;

import java.util.UUID;
import java.util.function.Predicate;

public class BeansBackpacksCompat implements InvModCompat<BeansBackpacksCompat.BeansBackpackInv> {
Expand All @@ -25,21 +27,21 @@ public String getModName() {

@Override
public void clear(ServerPlayerEntity player) {
BackData backData = BackData.get(player);

backData.set(ItemStack.EMPTY);
backData.backpackInventory.clear();
CompatHelper.setBackStack(player, ItemStack.EMPTY);
}

@Override
public CompatComponent<BeansBackpackInv> readNbt(NbtCompound nbt) {
ItemStack stack = ItemStack.fromNbt(nbt.getCompound("stack"));
DropRule rule = DropRule.valueOf(nbt.getString("dropRule"));
NbtCompound backpackContentsNbt = nbt.getCompound("backpackContents");
UUID ownerId = nbt.getUuid("ownerId");
float yaw = nbt.getFloat("yaw");
Direction direction = Direction.byId(nbt.getInt("direction"));

DefaultedList<ItemStack> backpackContents = InventoryComponent.listFromNbt(backpackContentsNbt, ItemStack::fromNbt, ItemStack.EMPTY);

BeansBackpackInv inv = new BeansBackpackInv(stack, rule, backpackContents);
BeansBackpackInv inv = new BeansBackpackInv(stack, rule, backpackContents, ownerId, yaw, direction);
return new BeansBackpacksComponent(inv);
}

Expand All @@ -52,11 +54,21 @@ public static class BeansBackpackInv {
private ItemStack stack;
private DropRule dropRule;
private DefaultedList<ItemStack> backpackContents;
private final UUID ownerId;

private final float yaw;
private final Direction direction;

public BeansBackpackInv(ItemStack stack, DropRule dropRule, DefaultedList<ItemStack> backpackContents) {
public BeansBackpackInv(ItemStack stack, DropRule dropRule, DefaultedList<ItemStack> backpackContents,
UUID ownerId, float yaw, Direction direction) {
this.stack = stack;
this.dropRule = dropRule;
this.backpackContents = backpackContents;
this.ownerId = ownerId;

// less important values, but they're cool I guess
this.yaw = yaw;
this.direction = direction;
}
public ItemStack getBackpack() {
return this.stack;
Expand Down Expand Up @@ -89,13 +101,11 @@ public BeansBackpacksComponent(BeansBackpackInv inventory) {

@Override
public BeansBackpackInv getInventory(ServerPlayerEntity player) {
BackData backData = BackData.get(player);
ItemStack stack = backData.getStack().copy();
DefaultedList<ItemStack> backpackContents = DefaultedList.of();
for (ItemStack item : backData.backpackInventory.getItemStacks()) {
backpackContents.add(item.copy());
}
return new BeansBackpackInv(stack, DropRule.PUT_IN_GRAVE, backpackContents);
ItemStack stack = CompatHelper.getBackStack(player).copy();
DefaultedList<ItemStack> backpackContents = CompatHelper.getBackpackInventory(player);

return new BeansBackpackInv(stack, DropRule.PUT_IN_GRAVE, backpackContents, player.getUuid(),
player.headYaw, player.getHorizontalFacing());
}

@Override
Expand All @@ -112,9 +122,9 @@ public DefaultedList<ItemStack> storeToPlayer(ServerPlayerEntity player) {
return extraItems;
}

BackData backData = BackData.get(player);
backData.set(backpack);
CompatHelper.setBackStack(player, backpack);

BackData backData = CompatHelper.getBackData(player);
backData.backpackInventory.clear();
for (ItemStack stack : backpackContents) {
if (!stack.isEmpty()) {
Expand All @@ -125,7 +135,7 @@ public DefaultedList<ItemStack> storeToPlayer(ServerPlayerEntity player) {
}
}

Services.NETWORK.backpackInventory2C(player);
CompatHelper.updateBackpackInventory2C(player);

return extraItems;
}
Expand Down Expand Up @@ -167,6 +177,10 @@ public NbtCompound writeNbt() {
NbtCompound nbt = new NbtCompound();
nbt.put("stack", this.inventory.stack.writeNbt(new NbtCompound()));
nbt.putString("dropRule", this.inventory.dropRule.name());
nbt.putUuid("ownerId", this.inventory.ownerId);

nbt.putFloat("yaw", this.inventory.yaw);
nbt.putInt("direction", this.inventory.direction.getId());

NbtCompound backpackContentsNbt = InventoryComponent.listToNbt(this.inventory.getBackpackContents(), itemStack -> itemStack.writeNbt(new NbtCompound()), ItemStack::isEmpty);
nbt.put("backpackContents", backpackContentsNbt);
Expand All @@ -184,22 +198,29 @@ public boolean removeItem(Predicate<ItemStack> predicate, int itemCount) {
return false;
}

// TODO: If it becomes possible, implement drop to drop the backpack entities
@Override
public void dropItems(ServerWorld world, Vec3d pos) {
super.dropItems(world, pos);
CompatHelper.createBackpackEntity(this.inventory.getBackpack(), (int) pos.x, (int) pos.y, (int) pos.z,
this.inventory.yaw, true, this.inventory.direction, world,
this.inventory.ownerId, this.inventory.getBackpackContents());
}
@Override
public void dropGraveItems(ServerWorld world, Vec3d pos) {
super.dropGraveItems(world, pos);
if (this.inventory.getDropRule() == DropRule.KEEP || this.inventory.getDropRule() == DropRule.DESTROY) return;

this.inventory.setDropRule(DropRule.DROP);
CompatHelper.createBackpackEntity(this.inventory.getBackpack(), (int) pos.x, (int) pos.y, (int) pos.z,
this.inventory.yaw, true, this.inventory.direction, world,
this.inventory.ownerId, this.inventory.getBackpackContents());
}

@Override
public CompatComponent<BeansBackpackInv> filterInv(Predicate<DropRule> predicate) {
if (predicate.test(this.inventory.getDropRule())) {
return new BeansBackpacksComponent(this.inventory);
} else {
return new BeansBackpacksComponent(new BeansBackpackInv(ItemStack.EMPTY, DropRule.PUT_IN_GRAVE, DefaultedList.of()));
return new BeansBackpacksComponent(new BeansBackpackInv(ItemStack.EMPTY, DropRule.PUT_IN_GRAVE,
DefaultedList.of(), this.inventory.ownerId, this.inventory.yaw, this.inventory.direction));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/b1n_ry/yigd/compat/CompatComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void dropItems(ServerWorld world, Vec3d pos) {
}

/**
* Drop all items in the component to the world, but only items that should be placed in a grave or dropped anyway
* Drop items in the component to the world, but only items that should be placed in a grave or dropped anyway
* @param world The world to drop items in
* @param pos The position to drop items at
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private void loseRandomItem() {

if (itemSlots.isEmpty()) return;

int random = this.RANDOM.nextInt(itemSlots.size());
int random = RANDOM.nextInt(itemSlots.size());

int slot = itemSlots.get(random);
if (itemLoss.affectStacks) {
Expand Down

0 comments on commit 0ec6d34

Please sign in to comment.