Fix not being able to add items when a single drive fills on types.

This commit is contained in:
SeanOMik 2021-07-04 11:42:38 -04:00
parent 021bb0e82d
commit be830f498b
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
4 changed files with 24 additions and 11 deletions

View File

@ -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>

View File

@ -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>

View File

@ -123,14 +123,28 @@ public class ESDrive implements Cloneable, ConfigurationSerializable {
}
}
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();

View File

@ -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 {
@ -144,7 +141,7 @@ public class ESSystem implements Cloneable, ConfigurationSerializable {
public ESDrive getNextAvailableDrive() {
for (ESDrive drive : esDrives) {
if (drive.isAvailable(null)) {
if (drive.canAddItem(null)) {
return drive;
}
}
@ -155,7 +152,9 @@ public class ESSystem implements Cloneable, ConfigurationSerializable {
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;
}
}
@ -185,7 +184,7 @@ public class ESSystem implements Cloneable, ConfigurationSerializable {
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();