1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

[CI skip] Reduced technical debt

This commit is contained in:
TheBusyBiscuit 2020-08-27 13:16:38 +02:00
parent b9b3d7f504
commit 4232caf17a
2 changed files with 126 additions and 112 deletions

View File

@ -83,6 +83,56 @@ final class CargoUtils {
return false;
}
static int[] getInputSlotRange(Inventory inv, ItemStack item) {
if (inv instanceof FurnaceInventory) {
if (item.getType().isFuel()) {
if (isSmeltable(item, true)) {
// Any non-smeltable items should not land in the upper slot
return new int[] { 0, 2 };
}
else {
return new int[] { 1, 2 };
}
}
else {
return new int[] { 0, 1 };
}
}
else if (inv instanceof BrewerInventory) {
if (isPotion(item)) {
// Slots for potions
return new int[] { 0, 3 };
}
else if (item != null && item.getType() == Material.BLAZE_POWDER) {
// Blaze Powder slot
return new int[] { 4, 5 };
}
else {
// Input slot
return new int[] { 3, 4 };
}
}
else {
// Slot 0-size
return new int[] { 0, inv.getSize() };
}
}
static int[] getOutputSlotRange(Inventory inv) {
if (inv instanceof FurnaceInventory) {
// Slot 2-3
return new int[] { 2, 3 };
}
else if (inv instanceof BrewerInventory) {
// Slot 0-3
return new int[] { 0, 3 };
}
else {
// Slot 0-size
return new int[] { 0, inv.getSize() };
}
}
static ItemStack withdraw(Map<Location, Inventory> inventories, Block node, Block target, ItemStack template) {
DirtyChestMenu menu = getChestMenu(target);
@ -129,16 +179,9 @@ final class CargoUtils {
static ItemStack withdrawFromVanillaInventory(Block node, ItemStack template, Inventory inv) {
ItemStack[] contents = inv.getContents();
int minSlot = 0;
int maxSlot = contents.length;
if (inv instanceof FurnaceInventory) {
minSlot = 2;
maxSlot = 3;
}
else if (inv instanceof BrewerInventory) {
maxSlot = 3;
}
int[] range = getOutputSlotRange(inv);
int minSlot = range[0];
int maxSlot = range[1];
ItemStackWrapper wrapper = new ItemStackWrapper(template);
@ -196,16 +239,9 @@ final class CargoUtils {
private static ItemStackAndInteger withdrawFromVanillaInventory(Block node, Inventory inv) {
ItemStack[] contents = inv.getContents();
int minSlot = 0;
int maxSlot = contents.length;
if (inv instanceof FurnaceInventory) {
minSlot = 2;
maxSlot = 3;
}
else if (inv instanceof BrewerInventory) {
maxSlot = 3;
}
int[] range = getOutputSlotRange(inv);
int minSlot = range[0];
int maxSlot = range[1];
for (int slot = minSlot; slot < maxSlot; slot++) {
ItemStack is = contents[slot];
@ -280,40 +316,9 @@ final class CargoUtils {
private static ItemStack insertIntoVanillaInventory(ItemStack stack, Inventory inv) {
ItemStack[] contents = inv.getContents();
int minSlot = 0;
int maxSlot = contents.length;
// Check if it is a normal furnace
if (inv instanceof FurnaceInventory) {
// Check if it is fuel or not
if (stack.getType().isFuel()) {
maxSlot = 2;
// Any non-smeltable items should not land in the upper slot
if (!isSmeltable(stack, true)) {
minSlot = 1;
}
}
else {
maxSlot = 1;
}
}
else if (inv instanceof BrewerInventory) {
if (stack.getType() == Material.POTION || stack.getType() == Material.LINGERING_POTION || stack.getType() == Material.SPLASH_POTION) {
// Potions slot
maxSlot = 3;
}
else if (stack.getType() == Material.BLAZE_POWDER) {
// Blaze Powder slot
minSlot = 4;
maxSlot = 5;
}
else {
// Input slot
minSlot = 3;
maxSlot = 4;
}
}
int[] range = getInputSlotRange(inv, stack);
int minSlot = range[0];
int maxSlot = range[1];
ItemStackWrapper wrapper = new ItemStackWrapper(stack);
@ -347,28 +352,6 @@ final class CargoUtils {
return stack;
}
/**
* This method checks if a given {@link ItemStack} is smeltable or not.
* The lazy-option is a performance-saver since actually calculating this can be quite expensive.
* For the current applicational purposes a quick check for any wooden logs is sufficient.
* Otherwise the "lazyness" can be turned off in the future.
*
* @param stack
* The {@link ItemStack} to test
* @param lazy
* Whether or not to perform a "lazy" but performance-saving check
*
* @return Whether the given {@link ItemStack} can be smelted or not
*/
private static boolean isSmeltable(ItemStack stack, boolean lazy) {
if (lazy) {
return stack != null && Tag.LOGS.isTagged(stack.getType());
}
else {
return SlimefunPlugin.getMinecraftRecipeService().isSmeltable(stack);
}
}
static DirtyChestMenu getChestMenu(Block block) {
if (BlockStorage.hasInventory(block)) {
return BlockStorage.getInventory(block);
@ -447,6 +430,32 @@ final class CargoUtils {
return defaultValue;
}
/**
* This method checks if a given {@link ItemStack} is smeltable or not.
* The lazy-option is a performance-saver since actually calculating this can be quite expensive.
* For the current applicational purposes a quick check for any wooden logs is sufficient.
* Otherwise the "lazyness" can be turned off in the future.
*
* @param stack
* The {@link ItemStack} to test
* @param lazy
* Whether or not to perform a "lazy" but performance-saving check
*
* @return Whether the given {@link ItemStack} can be smelted or not
*/
private static boolean isSmeltable(ItemStack stack, boolean lazy) {
if (lazy) {
return stack != null && Tag.LOGS.isTagged(stack.getType());
}
else {
return SlimefunPlugin.getMinecraftRecipeService().isSmeltable(stack);
}
}
private static boolean isPotion(ItemStack item) {
return item != null && (item.getType() == Material.POTION || item.getType() == Material.SPLASH_POTION || item.getType() == Material.LINGERING_POTION);
}
/**
* Get the whitelist/blacklist slots in a Cargo Input Node. If you wish to access the items
* in the cargo (without hardcoding the slots in case of change) then you can use this method.

View File

@ -368,41 +368,7 @@ abstract class ChestTerminalNetwork extends Network {
Optional<Block> block = getAttachedBlock(l);
if (block.isPresent()) {
Block target = block.get();
UniversalBlockMenu menu = BlockStorage.getUniversalInventory(target);
if (menu != null) {
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport((DirtyChestMenu) menu, ItemTransportFlow.WITHDRAW, null)) {
ItemStack is = menu.getItemInSlot(slot);
filter(is, items, l);
}
}
else if (BlockStorage.hasInventory(target)) {
BlockMenu blockMenu = BlockStorage.getInventory(target);
if (blockMenu.getPreset().getID().startsWith("BARREL_")) {
Config cfg = BlockStorage.getLocationInfo(target.getLocation());
String data = cfg.getString("storedItems");
if (data != null) {
gatherItemsFromBarrel(l, data, blockMenu, items);
}
}
else {
handleWithdraw(blockMenu, items, l);
}
}
else if (CargoUtils.hasInventory(target)) {
BlockState state = PaperLib.getBlockState(target, false).getState();
if (state instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) state).getInventory();
for (ItemStack is : inv.getContents()) {
filter(is, items, l);
}
}
}
findAllItems(items, l, block.get());
}
}
@ -410,8 +376,47 @@ abstract class ChestTerminalNetwork extends Network {
return items;
}
private void gatherItemsFromBarrel(Location l, String data, BlockMenu blockMenu, List<ItemStackAndInteger> items) {
private void findAllItems(List<ItemStackAndInteger> items, Location l, Block target) {
UniversalBlockMenu menu = BlockStorage.getUniversalInventory(target);
if (menu != null) {
for (int slot : menu.getPreset().getSlotsAccessedByItemTransport(menu, ItemTransportFlow.WITHDRAW, null)) {
ItemStack is = menu.getItemInSlot(slot);
filter(is, items, l);
}
}
else if (BlockStorage.hasInventory(target)) {
BlockMenu blockMenu = BlockStorage.getInventory(target);
if (blockMenu.getPreset().getID().startsWith("BARREL_")) {
gatherItemsFromBarrel(l, blockMenu, items);
}
else {
handleWithdraw(blockMenu, items, l);
}
}
else if (CargoUtils.hasInventory(target)) {
BlockState state = PaperLib.getBlockState(target, false).getState();
if (state instanceof InventoryHolder) {
Inventory inv = ((InventoryHolder) state).getInventory();
for (ItemStack is : inv.getContents()) {
filter(is, items, l);
}
}
}
}
private void gatherItemsFromBarrel(Location l, BlockMenu blockMenu, List<ItemStackAndInteger> items) {
try {
Config cfg = BlockStorage.getLocationInfo(blockMenu.getLocation());
String data = cfg.getString("storedItems");
if (data == null) {
return;
}
int stored = Integer.parseInt(data);
for (int slot : blockMenu.getPreset().getSlotsAccessedByItemTransport((DirtyChestMenu) blockMenu, ItemTransportFlow.WITHDRAW, null)) {