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> <groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId> <artifactId>energeticstorage</artifactId>
<name>EnergeticStorage</name> <name>EnergeticStorage</name>
<version>0.7.2-SNAPSHOT</version> <version>0.7.3-SNAPSHOT</version>
<build> <build>
<defaultGoal>clean package</defaultGoal> <defaultGoal>clean package</defaultGoal>
<resources> <resources>

View File

@ -6,7 +6,7 @@
<groupId>net.seanomik</groupId> <groupId>net.seanomik</groupId>
<artifactId>energeticstorage</artifactId> <artifactId>energeticstorage</artifactId>
<version>0.7.2-SNAPSHOT</version> <version>0.7.3-SNAPSHOT</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>EnergeticStorage</name> <name>EnergeticStorage</name>

View File

@ -123,14 +123,28 @@ public class ESDrive implements Cloneable, ConfigurationSerializable {
} }
} }
public boolean isAvailable(ItemStack item) { public boolean canAddItem(ItemStack item) {
return (Utils.isItemValid(item)) ? getFilledTypes() < Reference.MAX_DRIVE_TYPES && getFilledSpace() < size : getFilledSpace() < size; 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) { public boolean addItem(ItemStack item) {
item = item.clone(); item = item.clone();
if (isAvailable(item)) { if (canAddItem(item)) {
// The item is contained, then update the amount. // The item is contained, then update the amount.
if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) { if (Utils.containsSimilarItem(new ArrayList<>(items.keySet()), item, true)) {
int amount = (int) items.values().toArray()[Utils.indexOfSimilarItem(new ArrayList<>(items.keySet()), item)] + item.getAmount(); 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; package net.seanomik.energeticstorage.objects;
import net.seanomik.energeticstorage.utils.Utils; import net.seanomik.energeticstorage.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.configuration.serialization.ConfigurationSerializable; import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.Serializable;
import java.util.*; import java.util.*;
public class ESSystem implements Cloneable, ConfigurationSerializable { public class ESSystem implements Cloneable, ConfigurationSerializable {
@ -144,7 +141,7 @@ public class ESSystem implements Cloneable, ConfigurationSerializable {
public ESDrive getNextAvailableDrive() { public ESDrive getNextAvailableDrive() {
for (ESDrive drive : esDrives) { for (ESDrive drive : esDrives) {
if (drive.isAvailable(null)) { if (drive.canAddItem(null)) {
return drive; return drive;
} }
} }
@ -155,7 +152,9 @@ public class ESSystem implements Cloneable, ConfigurationSerializable {
public ESDrive findItemInAvailableDrive(ItemStack item) { public ESDrive findItemInAvailableDrive(ItemStack item) {
for (ESDrive drive : esDrives) { for (ESDrive drive : esDrives) {
for (ItemStack itemStack : drive.getItems().keySet()) { 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; return drive;
} }
} }
@ -185,7 +184,7 @@ public class ESSystem implements Cloneable, ConfigurationSerializable {
public boolean addItem(ItemStack item) { public boolean addItem(ItemStack item) {
ESDrive drive = findItemInAvailableDrive(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) { if (drive == null) {
drive = getNextAvailableDrive(); drive = getNextAvailableDrive();