Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/custom infinite quantum storage #164

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.sefiraat</groupId>
<artifactId>networks</artifactId>
<version>MODIFIED_1.2.0</version>
<version>MODIFIED_1.3.0</version>

<distributionManagement>
<repository>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ public class QuantumCache extends ItemStackCache {

@Nullable
private final ItemMeta storedItemMeta;
private final int limit;
private int limit;
private int amount;
private boolean voidExcess;
private boolean supportsCustomMaxAmount;

public QuantumCache(@Nullable ItemStack storedItem, int amount, int limit, boolean voidExcess) {
public QuantumCache(@Nullable ItemStack storedItem, int amount, int limit, boolean voidExcess, boolean supportsCustomMaxAmount) {
super(storedItem);
this.storedItemMeta = storedItem == null ? null : storedItem.getItemMeta();
this.amount = amount;
this.limit = limit;
this.voidExcess = voidExcess;
this.supportsCustomMaxAmount = supportsCustomMaxAmount;
}

@Nullable
Expand All @@ -33,10 +35,18 @@ public int getAmount() {
return amount;
}

public boolean getSupportsCustomMaxAmount() {
return supportsCustomMaxAmount;
}

public void setAmount(int amount) {
this.amount = amount;
}

public void setLimit(int limit) {
this.limit = limit;
}

public int increaseAmount(int amount) {
long total = (long) this.amount + (long) amount;
if (total > this.limit) {
Expand Down Expand Up @@ -92,15 +102,23 @@ public void addMetaLore(ItemMeta itemMeta) {
(this.getItemMeta() != null && this.getItemMeta().hasDisplayName() ? this.getItemMeta().getDisplayName() : this.getItemStack().getType().name())
);
lore.add(Theme.CLICK_INFO + "Amount: " + this.getAmount());
if (supportsCustomMaxAmount) {
lore.add(Theme.CLICK_INFO + "Max Amount: " + this.getLimit());
}

itemMeta.setLore(lore);
}

public void updateMetaLore(ItemMeta itemMeta) {
final List<String> lore = itemMeta.hasLore() ? itemMeta.getLore() : new ArrayList<>();
lore.set(lore.size() - 2,Theme.CLICK_INFO + "Holding: " +
final int loreIndexModifier = this.supportsCustomMaxAmount ? 1 : 0;
lore.set(lore.size() - 2 - loreIndexModifier,Theme.CLICK_INFO + "Holding: " +
(this.getItemMeta() != null && this.getItemMeta().hasDisplayName() ? this.getItemMeta().getDisplayName() : this.getItemStack().getType().name())
);
lore.set(lore.size() - 1, Theme.CLICK_INFO + "Amount: " + this.getAmount());
lore.set(lore.size() - 1 - loreIndexModifier, Theme.CLICK_INFO + "Amount: " + this.getAmount());
if (this.supportsCustomMaxAmount) {
lore.set(lore.size() - loreIndexModifier, Theme.CLICK_INFO + "Max Amount: " + this.getLimit());
}
itemMeta.setLore(lore);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,8 @@ public class NetworkSlimefunItems {
OPTIC_CABLE.getItem(), SlimefunItems.CARGO_MOTOR, OPTIC_CABLE.getItem(),
OPTIC_GLASS.getItem(), OPTIC_GLASS.getItem(), OPTIC_GLASS.getItem()
},
NetworkQuantumStorage.getSizes()[0]
NetworkQuantumStorage.getSizes()[0],
false
);

NETWORK_QUANTUM_STORAGE_2 = new NetworkQuantumStorage(
Expand All @@ -491,7 +492,8 @@ public class NetworkSlimefunItems {
SlimefunItems.SYNTHETIC_SAPPHIRE, NETWORK_QUANTUM_STORAGE_1.getItem(), SlimefunItems.SYNTHETIC_SAPPHIRE,
OPTIC_GLASS.getItem(), SlimefunItems.ALUMINUM_BRASS_INGOT, OPTIC_GLASS.getItem()
},
NetworkQuantumStorage.getSizes()[1]
NetworkQuantumStorage.getSizes()[1],
false
);

NETWORK_QUANTUM_STORAGE_3 = new NetworkQuantumStorage(
Expand All @@ -503,7 +505,8 @@ public class NetworkSlimefunItems {
SlimefunItems.SYNTHETIC_DIAMOND, NETWORK_QUANTUM_STORAGE_2.getItem(), SlimefunItems.SYNTHETIC_DIAMOND,
OPTIC_GLASS.getItem(), SlimefunItems.CORINTHIAN_BRONZE_INGOT, OPTIC_GLASS.getItem()
},
NetworkQuantumStorage.getSizes()[2]
NetworkQuantumStorage.getSizes()[2],
false
);

NETWORK_QUANTUM_STORAGE_4 = new NetworkQuantumStorage(
Expand All @@ -515,7 +518,8 @@ public class NetworkSlimefunItems {
SlimefunItems.SYNTHETIC_EMERALD, NETWORK_QUANTUM_STORAGE_3.getItem(), SlimefunItems.SYNTHETIC_EMERALD,
OPTIC_GLASS.getItem(), SlimefunItems.HARDENED_METAL_INGOT, OPTIC_GLASS.getItem()
},
NetworkQuantumStorage.getSizes()[3]
NetworkQuantumStorage.getSizes()[3],
false
);

NETWORK_QUANTUM_STORAGE_5 = new NetworkQuantumStorage(
Expand All @@ -527,7 +531,8 @@ public class NetworkSlimefunItems {
SlimefunItems.POWER_CRYSTAL, NETWORK_QUANTUM_STORAGE_4.getItem(), SlimefunItems.POWER_CRYSTAL,
OPTIC_GLASS.getItem(), SlimefunItems.REINFORCED_ALLOY_INGOT, OPTIC_GLASS.getItem()
},
NetworkQuantumStorage.getSizes()[4]
NetworkQuantumStorage.getSizes()[4],
false
);

NETWORK_QUANTUM_STORAGE_6 = new NetworkQuantumStorage(
Expand All @@ -539,7 +544,8 @@ public class NetworkSlimefunItems {
SlimefunItems.CARGO_MOTOR, NETWORK_QUANTUM_STORAGE_5.getItem(), SlimefunItems.CARGO_MOTOR,
SlimefunItems.STEEL_PLATE, SlimefunItems.BLISTERING_INGOT, SlimefunItems.STEEL_PLATE
},
NetworkQuantumStorage.getSizes()[5]
NetworkQuantumStorage.getSizes()[5],
false
);

NETWORK_QUANTUM_STORAGE_7 = new NetworkQuantumStorage(
Expand All @@ -551,7 +557,8 @@ public class NetworkSlimefunItems {
SlimefunItems.CARGO_CONNECTOR_NODE, NETWORK_QUANTUM_STORAGE_6.getItem(), SlimefunItems.CARGO_CONNECTOR_NODE,
SlimefunItems.REINFORCED_PLATE, SlimefunItems.BLISTERING_INGOT_2, SlimefunItems.REINFORCED_PLATE
},
NetworkQuantumStorage.getSizes()[6]
NetworkQuantumStorage.getSizes()[6],
false
);

NETWORK_QUANTUM_STORAGE_8 = new NetworkQuantumStorage(
Expand All @@ -563,7 +570,8 @@ public class NetworkSlimefunItems {
SlimefunItems.CARGO_MANAGER, NETWORK_QUANTUM_STORAGE_7.getItem(), SlimefunItems.CARGO_MANAGER,
OPTIC_GLASS.getItem(), SlimefunItems.BLISTERING_INGOT_3, OPTIC_GLASS.getItem()
},
NetworkQuantumStorage.getSizes()[7]
NetworkQuantumStorage.getSizes()[7],
true
);

NETWORK_CAPACITOR_1 = new NetworkPowerNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,7 @@ public class NetworksSlimefunItemStacks {
Theme.MACHINE,
"Network Quantum Storage (∞)",
"Stores ∞ items... almost",
"Has configurable maximum storage capacity",
"",
"Stores items in mass quantities within",
"a quantum singularity."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.github.thebusybiscuit.slimefun4.libraries.dough.data.persistent.PersistentDataAPI;
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.libraries.dough.protection.Interaction;
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import me.mrCookieSlime.CSCoreLibPlugin.Configuration.Config;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
Expand All @@ -41,6 +42,7 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class NetworkQuantumStorage extends SlimefunItem implements DistinctiveItem {
Expand All @@ -58,6 +60,7 @@ public class NetworkQuantumStorage extends SlimefunItem implements DistinctiveIt

public static final String BS_AMOUNT = "stored_amount";
public static final String BS_VOID = "void_excess";
public static final String BS_CUSTOM_MAX_AMOUNT = "custom_max_amount";

public static final int INPUT_SLOT = 1;
public static final int ITEM_SLOT = 4;
Expand Down Expand Up @@ -88,6 +91,14 @@ public class NetworkQuantumStorage extends SlimefunItem implements DistinctiveIt
Theme.PASSIVE + "Shift Click to change voiding"
);

private static final ItemStack SET_ITEM_SUPPORTING_CUSTOM_MAX = new CustomItemStack(
Material.LIME_STAINED_GLASS_PANE,
Theme.SUCCESS + "Set Item",
Theme.PASSIVE + "Drag an item on top of this pane to register it.",
Theme.PASSIVE + "Shift Click to change voiding",
Theme.PASSIVE + "Click with no item to set maximum storage size"
);

private static final ItemStack BACK_OUTPUT = new CustomItemStack(
Material.ORANGE_STAINED_GLASS_PANE,
Theme.PASSIVE + "Output"
Expand All @@ -108,10 +119,12 @@ public class NetworkQuantumStorage extends SlimefunItem implements DistinctiveIt

private final List<Integer> slotsToDrop = new ArrayList<>();
private final int maxAmount;
private final boolean supportsCustomMaxAmount;

public NetworkQuantumStorage(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, int maxAmount) {
public NetworkQuantumStorage(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, int maxAmount, boolean supportsCustomMaxAmount) {
super(itemGroup, item, recipeType, recipe);
this.maxAmount = maxAmount;
this.supportsCustomMaxAmount = supportsCustomMaxAmount;
slotsToDrop.add(INPUT_SLOT);
slotsToDrop.add(OUTPUT_SLOT);
}
Expand Down Expand Up @@ -217,6 +230,19 @@ private void setItem(@Nonnull BlockMenu blockMenu, @Nonnull Player player) {
CACHES.put(blockMenu.getLocation(), cache);
}

private void setCustomMaxAmount(@Nonnull BlockMenu blockMenu, @Nonnull Player player, @Nonnull int newMaxAmount) {
final QuantumCache cache = CACHES.get(blockMenu.getLocation());
if (cache == null || !cache.getSupportsCustomMaxAmount()) {
player.sendMessage(Theme.ERROR + "This Quantum Storage does not support custom maximum storage amounts.");
return;
}
cache.setLimit(newMaxAmount);
updateDisplayItem(blockMenu, cache);
syncBlock(blockMenu.getLocation(), cache);
CACHES.put(blockMenu.getLocation(), cache);
player.sendMessage(Theme.SUCCESS + "Maximum Storage Size applied");
}

@Override
public void postRegister() {
new BlockMenuPreset(this.getId(), this.getItemName()) {
Expand All @@ -232,7 +258,8 @@ public void init() {
for (int i : OUTPUT_SLOTS) {
addItem(i, BACK_OUTPUT, (p, slot, item, action) -> false);
}
addItem(ITEM_SET_SLOT, SET_ITEM, (p, slot, item, action) -> false);
ItemStack setItemItemstack = supportsCustomMaxAmount ? SET_ITEM_SUPPORTING_CUSTOM_MAX : SET_ITEM;
addItem(ITEM_SET_SLOT, setItemItemstack, (p, slot, item, action) -> false);
addMenuClickHandler(ITEM_SLOT, ChestMenuUtils.getEmptyClickHandler());
drawBackground(BACKGROUND_SLOTS);
}
Expand All @@ -255,8 +282,28 @@ public int[] getSlotsAccessedByItemTransport(ItemTransportFlow flow) {
@Override
public void newInstance(@Nonnull BlockMenu menu, @Nonnull Block block) {
menu.addMenuClickHandler(ITEM_SET_SLOT, (p, slot, item, action) -> {
final QuantumCache cache = CACHES.get(menu.getLocation());
if (action.isShiftClicked()) {
toggleVoid(menu);
} else if (
cache != null &&
cache.getSupportsCustomMaxAmount() &&
p.getItemOnCursor().getType() == Material.AIR
) {
p.closeInventory();
p.sendMessage(Theme.WARNING + "Type the new maximum storage size for this Quantum Storage.");
ChatUtils.awaitInput(p, s -> {
// Catching the error is cleaner than directly validating the string
try {
if (s.isBlank()) {
return;
}
int newMax = Math.max(1, Math.min(Integer.parseInt(s), maxAmount));
setCustomMaxAmount(menu, p, newMax);
} catch (NumberFormatException e) {
p.sendMessage(Theme.ERROR + "That's not a number.");
}
});
} else {
setItem(menu, p);
}
Expand All @@ -279,18 +326,26 @@ private QuantumCache addCache(@Nonnull BlockMenu blockMenu) {
final String voidString = BlockStorage.getLocationInfo(location, BS_VOID);
final int amount = amountString == null ? 0 : Integer.parseInt(amountString);
final boolean voidExcess = voidString == null || Boolean.parseBoolean(voidString);
// We use either the maximum amount if custom is not set, or the custom amount if it is set and is supported for this quantum
int customMaxAmount = this.maxAmount;
if (this.supportsCustomMaxAmount) {
final String customMaxAmountString = BlockStorage.getLocationInfo(blockMenu.getLocation(), BS_CUSTOM_MAX_AMOUNT);
if (customMaxAmountString != null) {
customMaxAmount = Integer.parseInt(customMaxAmountString);
}
}
final ItemStack itemStack = blockMenu.getItemInSlot(ITEM_SLOT);

QuantumCache cache = createCache(itemStack, blockMenu, amount, voidExcess);
QuantumCache cache = createCache(itemStack, blockMenu, amount, voidExcess, customMaxAmount);

CACHES.put(location, cache);
return cache;
}

private QuantumCache createCache(@Nullable ItemStack itemStack, @Nonnull BlockMenu menu, int amount, boolean voidExcess) {
private QuantumCache createCache(@Nullable ItemStack itemStack, @Nonnull BlockMenu menu, int amount, boolean voidExcess, int customMaxAmount) {
if (itemStack == null || itemStack.getType() == Material.AIR || isDisplayItem(itemStack)) {
menu.addItem(ITEM_SLOT, NO_ITEM);
return new QuantumCache(null, 0, this.maxAmount, true);
return new QuantumCache(null, 0, customMaxAmount, true, this.supportsCustomMaxAmount);
} else {
final ItemStack clone = itemStack.clone();
final ItemMeta itemMeta = clone.getItemMeta();
Expand All @@ -301,7 +356,7 @@ private QuantumCache createCache(@Nullable ItemStack itemStack, @Nonnull BlockMe
itemMeta.setLore(lore.isEmpty() ? null : lore);
clone.setItemMeta(itemMeta);

final QuantumCache cache = new QuantumCache(clone, amount, this.maxAmount, voidExcess);
final QuantumCache cache = new QuantumCache(clone, amount, customMaxAmount, voidExcess, this.supportsCustomMaxAmount);

updateDisplayItem(menu, cache);
return cache;
Expand Down Expand Up @@ -353,6 +408,10 @@ public int getMaxAmount() {
return maxAmount;
}

public boolean getSupportsCustomMaxAmount() {
return supportsCustomMaxAmount;
}

@ParametersAreNonnullByDefault
public static void tryInputItem(Location location, ItemStack[] input, QuantumCache cache) {
if (cache.getItemStack() == null) {
Expand Down Expand Up @@ -431,6 +490,12 @@ private static void updateDisplayItem(@Nonnull BlockMenu menu, @Nonnull QuantumC
lore.add("");
lore.add(Theme.CLICK_INFO + "Voiding: " + Theme.PASSIVE + StringUtils.toTitleCase(String.valueOf(cache.isVoidExcess())));
lore.add(Theme.CLICK_INFO + "Amount: " + Theme.PASSIVE + cache.getAmount());
if (cache.getSupportsCustomMaxAmount()) {
// Cache limit is set at the potentially custom max amount set
// The player could set the custom maximum amount to be the actual maximum amount
lore.add(Theme.CLICK_INFO + "Max Amount: " + Theme.PASSIVE + cache.getLimit());
}

itemMeta.setLore(lore);
itemStack.setItemMeta(itemMeta);
itemStack.setAmount(1);
Expand All @@ -441,6 +506,7 @@ private static void updateDisplayItem(@Nonnull BlockMenu menu, @Nonnull QuantumC
private static void syncBlock(@Nonnull Location location, @Nonnull QuantumCache cache) {
BlockStorage.addBlockInfo(location, BS_AMOUNT, String.valueOf(cache.getAmount()));
BlockStorage.addBlockInfo(location, BS_VOID, String.valueOf(cache.isVoidExcess()));
BlockStorage.addBlockInfo(location, BS_CUSTOM_MAX_AMOUNT, String.valueOf(cache.getLimit()));
}

public static Map<Location, QuantumCache> getCaches() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ public void craft(@Nonnull BlockMenu menu) {
oldCache.getItemStack().clone(),
oldCache.getAmount(),
newQuantum.getMaxAmount(),
oldCache.isVoidExcess()
oldCache.isVoidExcess(),
newQuantum.getSupportsCustomMaxAmount()
);
DataTypeMethods.setCustom(newMeta, Keys.QUANTUM_STORAGE_INSTANCE, PersistentQuantumStorageType.TYPE, newCache);
newCache.addMetaLore(newMeta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class PersistentQuantumStorageType implements PersistentDataType<Persiste
public static final NamespacedKey AMOUNT = Keys.newKey("amount");
public static final NamespacedKey MAX_AMOUNT = Keys.newKey("max_amount");
public static final NamespacedKey VOID = Keys.newKey("void");
public static final NamespacedKey SUPPORTS_CUSTOM_MAX_AMOUNT = Keys.newKey("supports_custom_max_amount");

@Override
@Nonnull
Expand All @@ -50,6 +51,7 @@ public PersistentDataContainer toPrimitive(@Nonnull QuantumCache complex, @Nonnu
container.set(AMOUNT, DataType.INTEGER, complex.getAmount());
container.set(MAX_AMOUNT, DataType.INTEGER, complex.getLimit());
container.set(VOID, DataType.BOOLEAN, complex.isVoidExcess());
container.set(SUPPORTS_CUSTOM_MAX_AMOUNT, DataType.BOOLEAN, complex.getSupportsCustomMaxAmount());
return container;
}

Expand All @@ -60,7 +62,8 @@ public QuantumCache fromPrimitive(@Nonnull PersistentDataContainer primitive, @N
final int amount = primitive.get(AMOUNT, DataType.INTEGER);
final int limit = primitive.get(MAX_AMOUNT, DataType.INTEGER);
final boolean voidExcess = primitive.get(VOID, DataType.BOOLEAN);
final boolean supportsCustomMaxAmount = primitive.get(SUPPORTS_CUSTOM_MAX_AMOUNT, DataType.BOOLEAN);

return new QuantumCache(item, amount, limit, voidExcess);
return new QuantumCache(item, amount, limit, voidExcess, supportsCustomMaxAmount);
}
}