Skip to content

Commit

Permalink
Fix bag
Browse files Browse the repository at this point in the history
  • Loading branch information
zabi94 committed Aug 20, 2024
1 parent 1f9b4f5 commit 98c756b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
33 changes: 23 additions & 10 deletions src/main/java/zabi/minecraft/extraalchemy/items/PotionBagItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import net.minecraft.client.item.TooltipType;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.ContainerComponent;
import net.minecraft.component.type.DyedColorComponent;
import net.minecraft.component.type.PotionContentsComponent;
import net.minecraft.entity.LivingEntity;
Expand All @@ -23,6 +24,7 @@
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.UseAction;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import zabi.minecraft.extraalchemy.client.tooltip.StatusEffectContainer;
import zabi.minecraft.extraalchemy.screen.potion_bag.BagInventory;
Expand All @@ -34,15 +36,13 @@

public class PotionBagItem extends Item implements StatusEffectContainer {

public static final String TAG_INVENTORY = "ea_inventory";
// public static final String TAG_LAST_CHANGE = "ea_changed";

private static final TagKey<Item> TAG_POTION = TagKey.of(Registries.ITEM.getKey(), LibMod.id("potion_for_bag"));

public PotionBagItem() {
super(new Item.Settings().maxCount(1)
.component(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT)
.component(ModComponents.SELECTION_MODE, SelectionMode.DESELECT)
.component(DataComponentTypes.CONTAINER, ContainerComponent.fromStacks(DefaultedList.ofSize(BagInventory.SLOT_AMOUNT, ItemStack.EMPTY)))
);
}

Expand Down Expand Up @@ -80,7 +80,8 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
} else {
handleRefill(stack);
PotionContentsComponent selectedPotion = stack.get(DataComponentTypes.POTION_CONTENTS);
if (getSelectedPotionAmount(stack).get() > 0 && selectedPotion.hasEffects() && selectedPotion.potion().isPresent()) {
Optional<Integer> optPotAmount = getSelectedPotionAmount(stack);
if (optPotAmount.isPresent() && optPotAmount.get() > 0 && selectedPotion.hasEffects() && selectedPotion.potion().isPresent()) {
user.setCurrentHand(hand);
}
user.getInventory().markDirty();
Expand All @@ -90,7 +91,10 @@ public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand han
}

private void handleRefill(ItemStack stack) {
if (getSelectedPotionAmount(stack).get() == 0) {

Optional<Integer> optPotAmount = getSelectedPotionAmount(stack);

if (optPotAmount.isEmpty() || optPotAmount.get() == 0) {
switch (getSelectionMode(stack)) {
case DESELECT:
selectPotion(stack, null);
Expand All @@ -108,11 +112,20 @@ private void handleRefill(ItemStack stack) {
}

public static void selectPotion(ItemStack bag, ItemStack potionStack) {

if (potionStack == null) {
bag.set(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT);
} else {
bag.set(DataComponentTypes.POTION_CONTENTS, potionStack.get(DataComponentTypes.POTION_CONTENTS));
return;
}

PotionContentsComponent pcc = potionStack.get(DataComponentTypes.POTION_CONTENTS);
if (pcc == null) {
bag.set(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT);
return;
}

bag.set(DataComponentTypes.POTION_CONTENTS, potionStack.get(DataComponentTypes.POTION_CONTENTS));

}

@Override
Expand Down Expand Up @@ -168,7 +181,7 @@ public boolean hasGlint(ItemStack stack) {

public Optional<PotionContentsComponent> getSelectedPotion(ItemStack bag) {
PotionContentsComponent selectedPotion = bag.get(DataComponentTypes.POTION_CONTENTS);
if (selectedPotion.hasEffects() && selectedPotion.potion().isPresent()) return Optional.empty();
if (!selectedPotion.hasEffects() || selectedPotion.potion().isEmpty()) return Optional.empty();
return Optional.of(selectedPotion);
}

Expand All @@ -179,7 +192,7 @@ public Optional<Integer> getSelectedPotionAmount(ItemStack bag) {
PotionContentsComponent target = potopt.get();
BagInventory inv = new BagInventory(bag, null);
for (int i = 0; i < inv.size(); i++) {
PotionContentsComponent currentPotion = inv.getStack(i).get(DataComponentTypes.POTION_CONTENTS);
PotionContentsComponent currentPotion = inv.getStack(i).getOrDefault(DataComponentTypes.POTION_CONTENTS, PotionContentsComponent.DEFAULT);
if (currentPotion.potion().isPresent() && target.matches(currentPotion.potion().get())) count++;
}
return Optional.of(count);
Expand Down Expand Up @@ -211,7 +224,7 @@ public Optional<ItemStack> getFirstAvailablePotion(ItemStack stack) {
for (int i = 0; i < inv.size(); i++) {
ItemStack currentStack = inv.getStack(i);
PotionContentsComponent currentPotion = currentStack.get(DataComponentTypes.POTION_CONTENTS);
if (currentPotion.hasEffects() && currentPotion.potion().isPresent()) {
if (currentPotion != null && currentPotion.hasEffects() && currentPotion.potion().isPresent()) {
return Optional.of(currentStack.copy());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

public class BagInventory implements Inventory {

private static final int SLOT_AMOUNT = 18;
public static final int SLOT_AMOUNT = 18;

private DefaultedList<ItemStack> inventory;
private DefaultedList<ItemStack> inventory = DefaultedList.ofSize(SLOT_AMOUNT, ItemStack.EMPTY);
private Hand openedWith;

public BagInventory(ItemStack bag, Hand hand) {
openedWith = hand;
ContainerComponent cc = bag.getOrDefault(DataComponentTypes.CONTAINER, ContainerComponent.fromStacks(DefaultedList.ofSize(SLOT_AMOUNT, ItemStack.EMPTY)));
ContainerComponent cc = bag.get(DataComponentTypes.CONTAINER);
cc.copyTo(inventory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ public void onSlotClick(int slotId, int mouseButton, SlotActionType actionType,
if (PotionUtilities.hasPotionEffects(iso)) {
ItemStack nis = iso.copy();
nis.setCount(1);
((Slot) slots.get(slotId)).setStackNoCallbacks(nis);
slots.get(slotId).setStackNoCallbacks(nis);
}
} else {
((Slot) slots.get(slotId)).setStackNoCallbacks(ItemStack.EMPTY);
slots.get(slotId).setStackNoCallbacks(ItemStack.EMPTY);
}

this.playerInventory.markDirty();
Expand Down Expand Up @@ -201,12 +201,15 @@ public int getMaxItemCount() {
@Override
public ItemStack getStack() {
Optional<PotionContentsComponent> selectedOpt = ModItems.POTION_BAG.getSelectedPotion(bagStack);
if (selectedOpt.isPresent()) {
ItemStack stack = new ItemStack(Registries.ITEM.getEntry(Items.POTION), 1, ComponentChanges.builder().add(DataComponentTypes.POTION_CONTENTS, selectedOpt.get()).build());
return stack;
} else {
if (selectedOpt.isEmpty()) {
return ItemStack.EMPTY;
}
PotionContentsComponent pcc = selectedOpt.get();
if (!pcc.hasEffects()) {
return ItemStack.EMPTY;
}
ItemStack stack = new ItemStack(Registries.ITEM.getEntry(Items.POTION), 1, ComponentChanges.builder().add(DataComponentTypes.POTION_CONTENTS, pcc).build());
return stack;
}

@Override
Expand Down

0 comments on commit 98c756b

Please sign in to comment.