Skip to content

Commit

Permalink
Reworked how reloading works in creative so that it aligns with vanil…
Browse files Browse the repository at this point in the history
…la behavior
  • Loading branch information
Desoroxxx committed Oct 14, 2024
1 parent 51d8de6 commit ae0eab7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ like knowing which weapon is better or whether an attachment will be useful to y
- Reworked magazines tooltips
- Reworked vests tooltips
- Updated Chinese translation
- Reworked how reloading works in creative so that it aligns with vanilla behavior

### Fixed

Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/paneedah/mwc/utils/MWCUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.paneedah.mwc.utils;

import com.paneedah.weaponlib.ItemMagazine;
import com.paneedah.weaponlib.Tags;
import com.paneedah.weaponlib.config.ModernConfigManager;
import io.redstudioragnarok.redcore.vectors.Vector3D;
import net.jafama.FastMath;
Expand Down Expand Up @@ -85,7 +86,7 @@ public static int consumeItemsFromPlayerInventory(final List<? extends Item> ite
return 0;
}

if (player.isCreative() && !player.isSneaking()) {
if (player.isCreative()) {
return amount;
}

Expand Down Expand Up @@ -129,20 +130,25 @@ public static int consumeItemsFromPlayerInventory(final List<? extends Item> ite
public static ItemStack consumeItemsFromPlayerInventory(final List<? extends ItemMagazine> items, final Comparator<ItemStack> comparator, final EntityPlayer player) {
ItemStack maxStack = null;

if (player.isCreative() && !player.isSneaking()) {
return items.stream().map(ItemMagazine::create).max(comparator).orElse(null);
}

for (final ItemStack currentStack : player.inventory.mainInventory)
if (items.contains(currentStack.getItem()) && (maxStack == null || comparator.compare(currentStack, maxStack) > 0)) {
maxStack = currentStack;
}

if (maxStack == null || maxStack.isEmpty()) {
if (player.isCreative())
return items.stream().map(ItemMagazine::create).max(comparator).orElse(null);

return null;
}

return maxStack.splitStack(1);
if (!player.isCreative()) {
maxStack = maxStack.splitStack(1);
} else {
Tags.setAmmo(maxStack, ((ItemMagazine) maxStack.getItem()).getCapacity());
}

return maxStack;
}

/**
Expand Down
22 changes: 13 additions & 9 deletions src/main/java/com/paneedah/weaponlib/WeaponReloadAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -504,10 +504,6 @@ private ItemAttachment<Weapon> getNextMagazine(PlayerWeaponInstance weaponInstan
Comparator<ItemStack> comparator;
comparator = (stack1, stack2) -> Integer.compare(Tags.getAmmo(stack1), Tags.getAmmo(stack2));

if (player.isCreative() && !player.isSneaking()) {
return (ItemAttachment<Weapon>) compatibleMagazines.stream().map(ItemMagazine::create).max(comparator).orElse(null).getItem();
}

int maxItemIndex = -1;
ItemStack maxStack = null;

Expand Down Expand Up @@ -537,6 +533,9 @@ private ItemAttachment<Weapon> getNextMagazine(PlayerWeaponInstance weaponInstan
int i = maxItemIndex;

if (i < 0) {
if (player.isCreative())
return (ItemAttachment<Weapon>) compatibleMagazines.stream().map(ItemMagazine::create).max(comparator).orElse(null).getItem();

return null;
}

Expand All @@ -558,10 +557,6 @@ private ItemStack getNextBestMagazineStack(PlayerWeaponInstance weaponInstance)
Comparator<ItemStack> comparator;
comparator = (stack1, stack2) -> Integer.compare(Tags.getAmmo(stack1), Tags.getAmmo(stack2));

if (player.isCreative() && !player.isSneaking()) {
return compatibleMagazines.stream().map(ItemMagazine::create).max(comparator).orElse(null);
}

int maxItemIndex = -1;
ItemStack maxStack = null;

Expand Down Expand Up @@ -591,10 +586,19 @@ private ItemStack getNextBestMagazineStack(PlayerWeaponInstance weaponInstance)
int i = maxItemIndex;

if (i < 0) {
if (player.isCreative())
return compatibleMagazines.stream().map(ItemMagazine::create).max(comparator).orElse(null);

return null;
}

ItemStack magazineItemStack = player.inventory.getStackInSlot(i).copy().splitStack(Math.min(player.inventory.getStackInSlot(i).copy().getCount(), 1));
ItemStack magazineItemStack = player.inventory.getStackInSlot(i).copy();

if (!player.isCreative()){
magazineItemStack = magazineItemStack.splitStack(Math.min(player.inventory.getStackInSlot(i).copy().getCount(), 1));
} else {
Tags.setAmmo(magazineItemStack, ((ItemMagazine) magazineItemStack.getItem()).getCapacity());
}

return magazineItemStack;
}
Expand Down

0 comments on commit ae0eab7

Please sign in to comment.