Skip to content

Commit

Permalink
Fix not being able to add items when a single drive fills on types.
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanOMik committed Jul 4, 2021
1 parent 021bb0e commit be830f4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId>
<name>EnergeticStorage</name>
<version>0.7.2-SNAPSHOT</version>
<version>0.7.3-SNAPSHOT</version>
<build>
<defaultGoal>clean package</defaultGoal>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId>
<version>0.7.2-SNAPSHOT</version>
<version>0.7.3-SNAPSHOT</version>
<packaging>jar</packaging>

<name>EnergeticStorage</name>
Expand Down
20 changes: 17 additions & 3 deletions src/main/java/net/seanomik/energeticstorage/objects/ESDrive.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,28 @@ public ESDrive clone() {
}
}

public boolean isAvailable(ItemStack item) {
return (Utils.isItemValid(item)) ? getFilledTypes() < Reference.MAX_DRIVE_TYPES && getFilledSpace() < size : getFilledSpace() < size;
public boolean canAddItem(ItemStack item) {
if (Utils.isItemValid(item)) {
// If the item is valid, we're full on types, we have the item in the drive, and we're not full on space, return true.
// else always just cascase down and check if we have space.
if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) {
return getFilledSpace() < size;
} else {
if (getFilledTypes() < Reference.MAX_DRIVE_TYPES) {
return getFilledSpace() < size;
}

return false;
}
} else {
return getFilledSpace() < size;
}
}

public boolean addItem(ItemStack item) {
item = item.clone();

if (isAvailable(item)) {
if (canAddItem(item)) {
// The item is contained, then update the amount.
if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) {
int amount = (int) items.values().toArray()[Utils.indexOfSimilarItem(new ArrayList<>(items.keySet()), item)] + item.getAmount();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package net.seanomik.energeticstorage.objects;

import net.seanomik.energeticstorage.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;
import java.util.*;

public class ESSystem implements Cloneable, ConfigurationSerializable {
Expand Down Expand Up @@ -144,7 +141,7 @@ public boolean equals(Object other) {

public ESDrive getNextAvailableDrive() {
for (ESDrive drive : esDrives) {
if (drive.isAvailable(null)) {
if (drive.canAddItem(null)) {
return drive;
}
}
Expand All @@ -155,7 +152,9 @@ public ESDrive getNextAvailableDrive() {
public ESDrive findItemInAvailableDrive(ItemStack item) {
for (ESDrive drive : esDrives) {
for (ItemStack itemStack : drive.getItems().keySet()) {
if (item.isSimilar(itemStack) && drive.isAvailable(item)) {
// We don't need to check if we can add the item since if its
// added, then i
if (item.isSimilar(itemStack) && drive.canAddItem(null)) {
return drive;
}
}
Expand Down Expand Up @@ -185,7 +184,7 @@ public Map<ItemStack, Integer> getAllItems() {
public boolean addItem(ItemStack item) {
ESDrive drive = findItemInAvailableDrive(item);

// If we failed to find the item in the next available drive, then find another drive.
// If we failed to find the item in the next available drive, then find another drive to add it to.
if (drive == null) {
drive = getNextAvailableDrive();

Expand Down

0 comments on commit be830f4

Please sign in to comment.