Skip to content

Commit

Permalink
Added exclude functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
pcluddite committed Sep 3, 2019
1 parent d53fc7f commit 93950d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
40 changes: 28 additions & 12 deletions src/main/java/tbax/baxshops/PlayerUtil.java
Expand Up @@ -25,6 +25,7 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import tbax.baxshops.errors.CommandErrorException;
import tbax.baxshops.errors.CommandWarningException;
import tbax.baxshops.errors.PrematureAbortException;
Expand All @@ -33,10 +34,7 @@
import tbax.baxshops.items.ItemUtil;
import tbax.baxshops.serialization.StoredPlayer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.*;

/**
* Methods for dealing with interactions with players
Expand Down Expand Up @@ -248,7 +246,7 @@ public static void sellItem(@NotNull UUID shopId, @NotNull UUID buyer, @NotNull
*/
public static @NotNull List<BaxEntry> takeQtyFromInventory(@NotNull BaxQuantity qty) throws PrematureAbortException
{
return takeQtyFromInventory(qty, null);
return takeQtyFromInventory(qty, null, Collections.emptyList());
}

/**
Expand All @@ -258,12 +256,13 @@ public static void sellItem(@NotNull UUID shopId, @NotNull UUID buyer, @NotNull
* @return a list of BaxEntries with the removed items
* @throws PrematureAbortException thrown when any is specified and shop is null or inv is null
*/
public static @NotNull List<BaxEntry> takeQtyFromInventory(@NotNull BaxQuantity qty, BaxShop shop) throws PrematureAbortException
public static @NotNull List<BaxEntry> takeQtyFromInventory(@NotNull BaxQuantity qty, @Nullable BaxShop shop,
@Nullable List<BaxEntry> exclude) throws PrematureAbortException
{
if (qty.isAny()) {
if (!(qty.getInventory() instanceof PlayerInventory))
throw new CommandErrorException("'any' cannot be used for this action");
return takeAnyFromInventory(shop, (PlayerInventory)qty.getInventory());
return takeAnyFromInventory(shop, (PlayerInventory)qty.getInventory(), exclude);
}

BaxEntry clone = null;
Expand All @@ -289,12 +288,13 @@ public static void sellItem(@NotNull UUID shopId, @NotNull UUID buyer, @NotNull
* @return a list of BaxEntries with the removed items
* @throws PrematureAbortException thrown when any is specified and shop is null or inv is null
*/
public static List<BaxEntry> peekQtyFromInventory(@NotNull BaxQuantity qty, BaxShop shop) throws PrematureAbortException
public static List<BaxEntry> peekQtyFromInventory(@NotNull BaxQuantity qty, BaxShop shop, @Nullable List<BaxEntry> exclude)
throws PrematureAbortException
{
if (qty.isAny()) {
if (!(qty.getInventory() instanceof PlayerInventory))
throw new CommandErrorException("'any' cannot be used for this action");
return peekAnyFromInventory(shop, (PlayerInventory)qty.getInventory());
return peekAnyFromInventory(shop, (PlayerInventory)qty.getInventory(), exclude);
}

BaxEntry clone = null;
Expand All @@ -313,11 +313,19 @@ public static List<BaxEntry> peekQtyFromInventory(@NotNull BaxQuantity qty, BaxS
return Collections.singletonList(clone);
}

private static @NotNull List<BaxEntry> takeAnyFromInventory(@NotNull BaxShop shop, @NotNull PlayerInventory inv)
private static @NotNull List<BaxEntry> takeAnyFromInventory(@NotNull BaxShop shop, @NotNull PlayerInventory inv,
@Nullable List<BaxEntry> exclude)
{
ArrayList<BaxEntry> list = new ArrayList<>();

if (exclude == null)
exclude = Collections.emptyList();

for (BaxEntry entry : shop) {

if (exclude.contains(entry))
continue;

BaxEntry curr = new BaxEntry(entry);
curr.setAmount(0);
for (int x = 0; x < inv.getSize(); ++x) {
Expand All @@ -335,11 +343,19 @@ public static List<BaxEntry> peekQtyFromInventory(@NotNull BaxQuantity qty, BaxS
return list;
}

private static @NotNull List<BaxEntry> peekAnyFromInventory(@NotNull BaxShop shop, @NotNull PlayerInventory inv)
private static @NotNull List<BaxEntry> peekAnyFromInventory(@NotNull BaxShop shop, @NotNull PlayerInventory inv,
@Nullable List<BaxEntry> exclude)
{
ArrayList<BaxEntry> list = new ArrayList<>();
List<BaxEntry> list = new ArrayList<>();

if (exclude == null)
exclude = Collections.emptyList();

for (BaxEntry entry : shop) {

if (exclude.contains(entry))
continue;

BaxEntry curr = new BaxEntry(entry);
curr.setAmount(0);
for (int x = 0; x < inv.getSize(); ++x) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/tbax/baxshops/commands/ShopCmdActor.java
Expand Up @@ -80,11 +80,11 @@ public void setArgs(String[] args)
this.args = argList.toArray(new String[0]);
}

public @Nullable BaxEntry getExcluded() throws PrematureAbortException
public @Nullable List<BaxEntry> getExcluded() throws PrematureAbortException
{
if (excluded == null)
return null;
return getEntryFromString(excluded, "Excluded item not found in shop");
return Collections.singletonList(getEntryFromString(excluded, "Excluded item not found in shop"));
}

public String[] getArgs()
Expand Down Expand Up @@ -532,12 +532,12 @@ public ItemStack getItemInHand()

public List<BaxEntry> takeArgFromInventory(int index) throws PrematureAbortException
{
return PlayerUtil.takeQtyFromInventory(getArgPlayerQty(index), getShop());
return PlayerUtil.takeQtyFromInventory(getArgPlayerQty(index), getShop(), getExcluded());
}

public List<BaxEntry> peekArgFromInventory(int index) throws PrematureAbortException
{
return PlayerUtil.peekQtyFromInventory(getArgPlayerQty(index), getShop());
return PlayerUtil.peekQtyFromInventory(getArgPlayerQty(index), getShop(), getExcluded());
}

public PlayerInventory getInventory()
Expand Down

0 comments on commit 93950d6

Please sign in to comment.