From d4285df03045ba0ab65ad1ffeaaca5855e38804d Mon Sep 17 00:00:00 2001 From: koolkrafter5 Date: Fri, 9 May 2025 14:06:16 -0400 Subject: [PATCH] Refactor the Orchestra of the Phantom Hands ritual. Functionality should be identical, but there is a lot less nesting now. --- .../api/RoutingFocusParadigm.java | 4 +- .../items/routing/OutputRoutingFocus.java | 69 ++-- .../rituals/RitualEffectItemRouting.java | 309 +++++++----------- 3 files changed, 148 insertions(+), 234 deletions(-) diff --git a/src/main/java/WayofTime/alchemicalWizardry/api/RoutingFocusParadigm.java b/src/main/java/WayofTime/alchemicalWizardry/api/RoutingFocusParadigm.java index 4f5c6cc3..23144645 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/api/RoutingFocusParadigm.java +++ b/src/main/java/WayofTime/alchemicalWizardry/api/RoutingFocusParadigm.java @@ -7,9 +7,9 @@ import net.minecraft.item.ItemStack; public class RoutingFocusParadigm { - public List logicList = new LinkedList(); + public List logicList = new LinkedList<>(); - public List locationList = new LinkedList(); + public List locationList = new LinkedList<>(); public int maximumAmount = 0; diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/items/routing/OutputRoutingFocus.java b/src/main/java/WayofTime/alchemicalWizardry/common/items/routing/OutputRoutingFocus.java index 76879eda..70d5d6e4 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/items/routing/OutputRoutingFocus.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/items/routing/OutputRoutingFocus.java @@ -55,49 +55,32 @@ public class OutputRoutingFocus extends RoutingFocus implements ILimitedRoutingF @SideOnly(Side.CLIENT) public IIcon getIconFromDamage(int damage) { - switch (damage) { - case 0: - return this.itemIcon; - case 1: - return this.modItemIcon; - case 2: - return this.ignMetaIcon; - case 3: - return this.matchNBTIcon; - case 4: - return this.globalIcon; - } - return this.itemIcon; + return switch (damage) { + case 1 -> this.modItemIcon; + case 2 -> this.ignMetaIcon; + case 3 -> this.matchNBTIcon; + case 4 -> this.globalIcon; + default -> this.itemIcon; + }; } @Override public String getUnlocalizedName(ItemStack itemStack) { - String addedString = ""; - - switch (itemStack.getItemDamage()) { - case 0: - addedString = "default"; - break; - case 1: - addedString = "modItem"; - break; - case 2: - addedString = "ignMeta"; - break; - case 3: - addedString = "matchNBT"; - break; - case 4: - addedString = "global"; - break; - } + String addedString = switch (itemStack.getItemDamage()) { + case 0 -> "default"; + case 1 -> "modItem"; + case 2 -> "ignMeta"; + case 3 -> "matchNBT"; + case 4 -> "global"; + default -> ""; + }; return super.getUnlocalizedName() + "." + addedString; } @Override @SideOnly(Side.CLIENT) - public void getSubItems(Item id, CreativeTabs creativeTab, List list) { + public void getSubItems(Item id, CreativeTabs creativeTab, List list) { for (int meta = 0; meta < 5; ++meta) { list.add(new ItemStack(id, 1, meta)); } @@ -106,18 +89,14 @@ public class OutputRoutingFocus extends RoutingFocus implements ILimitedRoutingF @Override public RoutingFocusLogic getLogic(ItemStack itemStack) { if (itemStack != null) { - switch (itemStack.getItemDamage()) { - case 0: - return new RoutingFocusLogicLimitDefault(itemStack); - case 1: - return new RoutingFocusLogicLimitModItems(itemStack); - case 2: - return new RoutingFocusLogicLimitIgnMeta(itemStack); - case 3: - return new RoutingFocusLogicLimitMatchNBT(itemStack); - case 4: - return new RoutingFocusLogicLimitGlobal(itemStack); - } + return switch (itemStack.getItemDamage()) { + case 0 -> new RoutingFocusLogicLimitDefault(itemStack); + case 1 -> new RoutingFocusLogicLimitModItems(itemStack); + case 2 -> new RoutingFocusLogicLimitIgnMeta(itemStack); + case 3 -> new RoutingFocusLogicLimitMatchNBT(itemStack); + case 4 -> new RoutingFocusLogicLimitGlobal(itemStack); + default -> new RoutingFocusLogic(); + }; } return new RoutingFocusLogic(); diff --git a/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectItemRouting.java b/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectItemRouting.java index e2432598..41e145f8 100644 --- a/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectItemRouting.java +++ b/src/main/java/WayofTime/alchemicalWizardry/common/rituals/RitualEffectItemRouting.java @@ -1,10 +1,8 @@ package WayofTime.alchemicalWizardry.common.rituals; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.function.Function; import net.minecraft.inventory.IInventory; import net.minecraft.inventory.ISidedInventory; @@ -20,213 +18,150 @@ import WayofTime.alchemicalWizardry.api.RoutingFocusPosAndFacing; import WayofTime.alchemicalWizardry.api.rituals.IMasterRitualStone; import WayofTime.alchemicalWizardry.api.rituals.RitualComponent; import WayofTime.alchemicalWizardry.api.rituals.RitualEffect; -import WayofTime.alchemicalWizardry.api.soulNetwork.SoulNetworkHandler; import WayofTime.alchemicalWizardry.common.items.routing.InputRoutingFocus; import WayofTime.alchemicalWizardry.common.items.routing.OutputRoutingFocus; import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper; public class RitualEffectItemRouting extends RitualEffect { - Random rand = new Random(); + private static final Int3[] INPUT_BUFFER_LOCATIONS = { new Int3(1, 0, 0), new Int3(-1, 0, 0), new Int3(0, 0, 1), + new Int3(0, 0, -1) }; + + private static final Int3[] OUTPUT_BUFFER_LOCATIONS = { new Int3(2, 0, 2), new Int3(-2, 0, 2), new Int3(2, 0, -2), + new Int3(-2, 0, -2) }; @Override public void performEffect(IMasterRitualStone ritualStone) { - String owner = ritualStone.getOwner(); - - int currentEssence = SoulNetworkHandler.getCurrentEssence(owner); World world = ritualStone.getWorld(); int x = ritualStone.getXCoord(); int y = ritualStone.getYCoord(); int z = ritualStone.getZCoord(); - if (world.getTotalWorldTime() % 20 != 0) { - return; + if (world.getTotalWorldTime() % 20 != 0) return; + + List outputChestList = findBufferChests(world, x, y, z, OUTPUT_BUFFER_LOCATIONS); + if (outputChestList.isEmpty()) return; + + List inputFocusChests = findBufferChests(world, x, y, z, INPUT_BUFFER_LOCATIONS); + if (inputFocusChests.isEmpty()) return; + + for (IInventory outputFocusChests : outputChestList) { + processOutputFocusInventory(world, outputFocusChests, inputFocusChests); } + } - Map tileMap = new HashMap(); + private void processOutputFocusInventory(World world, IInventory outputFocusChests, + List inputFocusChests) { + RoutingFocusParadigm paradigm = new RoutingFocusParadigm(); + boolean lastItemWasFocus = true; - List outputList = new ArrayList<>(); - for (int i = 0; i < 4; i++) // Check output foci chests, return if none available - { - Int3 outputFocusChest = this.getOutputBufferChestLocation(i); - TileEntity outputFocusInv = world.getTileEntity( - x + outputFocusChest.xCoord, - y + outputFocusChest.yCoord, - z + outputFocusChest.zCoord); - if (outputFocusInv instanceof IInventory) { - outputList.add((IInventory) outputFocusInv); + for (int i = 0; i < outputFocusChests.getSizeInventory(); i++) { + ItemStack keyStack = outputFocusChests.getStackInSlot(i); + if (keyStack == null) continue; + + if (keyStack.getItem() instanceof OutputRoutingFocus outputFocus) { + if (!lastItemWasFocus) paradigm.clear(); + paradigm.addRoutingFocusPosAndFacing(outputFocus.getPosAndFacing(keyStack)); + paradigm.addLogic(outputFocus.getLogic(keyStack)); + lastItemWasFocus = true; + continue; } - } + lastItemWasFocus = false; - if (outputList.isEmpty()) { - return; - } + for (RoutingFocusPosAndFacing posAndFacing : paradigm.locationList) { + if (posAndFacing == null) continue; + ForgeDirection inputDirection = posAndFacing.facing; + TileEntity outputChest = world.getTileEntity( + posAndFacing.location.xCoord, + posAndFacing.location.yCoord, + posAndFacing.location.zCoord); - for (IInventory outputFocusInventory : outputList) { - OutputRoutingFocus outputFocus; + if (!(outputChest instanceof IInventory outputChestInventory)) continue; - RoutingFocusParadigm parad = new RoutingFocusParadigm(); - - TileEntity outputChest = null; // Destination - ForgeDirection inputDirection; - - IInventory outputChestInventory = null; - - boolean lastItemWasFocus = true; - - for (int j = 0; j < outputFocusInventory.getSizeInventory(); j++) { - ItemStack keyStack = outputFocusInventory.getStackInSlot(j); - if (keyStack == null) { - continue; - } - - if (keyStack.getItem() instanceof OutputRoutingFocus) { - if (!lastItemWasFocus) { - parad.clear(); - } - - outputFocus = (OutputRoutingFocus) keyStack.getItem(); - - parad.addRoutingFocusPosAndFacing(outputFocus.getPosAndFacing(keyStack)); - parad.addLogic(outputFocus.getLogic(keyStack)); - lastItemWasFocus = true; - continue; - } else { - lastItemWasFocus = false; - } - - for (RoutingFocusPosAndFacing posAndFacing : parad.locationList) { - if (posAndFacing == null) { - continue; - } - inputDirection = posAndFacing.facing; - if (outputChest == null || !posAndFacing.location - .equals(new Int3(outputChest.xCoord, outputChest.yCoord, outputChest.zCoord))) { - outputChest = world.getTileEntity( - posAndFacing.location.xCoord, - posAndFacing.location.yCoord, - posAndFacing.location.zCoord); - if (outputChest instanceof IInventory) { - outputChestInventory = (IInventory) outputChest; - } else { - continue; - } - } - - for (int i = 0; i < 4; i++) { - Int3 inputFocusChest = this.getInputBufferChestLocation(i); - TileEntity inputFocusInv = world.getTileEntity( - x + inputFocusChest.xCoord, - y + inputFocusChest.yCoord, - z + inputFocusChest.zCoord); - if (inputFocusInv instanceof IInventory) { - for (int ji = 0; ji < ((IInventory) inputFocusInv).getSizeInventory(); ji++) // Iterate - // through - // foci - // inventory - { - ItemStack inputFocusStack = ((IInventory) inputFocusInv).getStackInSlot(ji); - if (inputFocusStack != null && inputFocusStack.getItem() instanceof InputRoutingFocus inputFocus) { - TileEntity inputChest = world.getTileEntity( - inputFocus.xCoord(inputFocusStack), - inputFocus.yCoord(inputFocusStack), - inputFocus.zCoord(inputFocusStack)); - if (inputChest instanceof IInventory inputChestInventory) { - ForgeDirection syphonDirection = inputFocus.getSetDirection(inputFocusStack); - boolean[] canSyphonList = new boolean[inputChestInventory.getSizeInventory()]; - if (inputChest instanceof ISidedInventory) { - int[] validSlots = ((ISidedInventory) inputChest) - .getAccessibleSlotsFromSide(syphonDirection.ordinal()); - for (int in : validSlots) { - canSyphonList[in] = true; - } - } else { - for (int ni = 0; ni < inputChestInventory.getSizeInventory(); ni++) { - canSyphonList[ni] = true; - } - } - - for (int ni = 0; ni < inputChestInventory.getSizeInventory(); ni++) { - if (canSyphonList[ni]) { - // Has a syphoned item linked, next need to find a destination - ItemStack syphonedStack = inputChestInventory.getStackInSlot(ni); - if (syphonedStack == null - || (inputChestInventory instanceof ISidedInventory - && !((ISidedInventory) inputChestInventory) - .canExtractItem( - ni, - syphonedStack, - syphonDirection.ordinal()))) { - continue; - } - - int size = syphonedStack.stackSize; - - if (parad.doesItemMatch(keyStack, syphonedStack)) { - ItemStack newStack = null; - if (parad.maximumAmount <= 0) { - newStack = SpellHelper.insertStackIntoInventory( - syphonedStack, - outputChestInventory, - inputDirection); - } else { - newStack = SpellHelper.insertStackIntoInventory( - syphonedStack, - outputChestInventory, - inputDirection, - parad.maximumAmount); - } - if (size == newStack.stackSize) { - continue; - } - - int numberSyphoned = size - newStack.stackSize; - - if (newStack != null && newStack.stackSize <= 0) { - size = newStack.stackSize; - newStack = null; - } - inputChestInventory.setInventorySlotContents(ni, newStack); - // break; - } - } - } - } - } - } - } - } + for (IInventory inputFocusInventory : inputFocusChests) { + processInputFocusInventory( + world, + inputFocusInventory, + keyStack, + paradigm, + outputChestInventory, + inputDirection); } } } } - public Int3 getInputBufferChestLocation(int number) { - switch (number) { - case 0: - return new Int3(1, 0, 0); - case 1: - return new Int3(-1, 0, 0); - case 2: - return new Int3(0, 0, 1); - case 3: - return new Int3(0, 0, -1); + private void processInputFocusInventory(World world, IInventory inputFocusInventory, ItemStack keyStack, + RoutingFocusParadigm paradigm, IInventory outputChestInventory, ForgeDirection inputDirection) { + for (int i = 0; i < inputFocusInventory.getSizeInventory(); i++) { + ItemStack inputFocusStack = inputFocusInventory.getStackInSlot(i); + if (inputFocusStack == null || !(inputFocusStack.getItem() instanceof InputRoutingFocus inputFocus)) + continue; + + TileEntity inputChest = world.getTileEntity( + inputFocus.xCoord(inputFocusStack), + inputFocus.yCoord(inputFocusStack), + inputFocus.zCoord(inputFocusStack)); + + if (!(inputChest instanceof IInventory inputChestInventory)) continue; + + boolean[] canSyphonList = getSyphonableSlots( + inputChestInventory, + inputFocus.getSetDirection(inputFocusStack)); + + for (int j = 0; j < inputChestInventory.getSizeInventory(); j++) { + if (!canSyphonList[j]) continue; + ItemStack syphonedStack = inputChestInventory.getStackInSlot(j); + if (!canExtractItem(inputChestInventory, j, syphonedStack, inputFocus.getSetDirection(inputFocusStack))) + continue; + + if (paradigm.doesItemMatch(keyStack, syphonedStack)) { + ItemStack newStack = insertStackWithLimit( + syphonedStack, + outputChestInventory, + inputDirection, + paradigm.maximumAmount); + inputChestInventory.setInventorySlotContents( + j, + (newStack != null && newStack.stackSize > 0) ? newStack : null); + } + } } - return new Int3(0, 0, 0); } - public Int3 getOutputBufferChestLocation(int number) { - switch (number) { - case 0: - return new Int3(2, 0, 2); - case 1: - return new Int3(-2, 0, 2); - case 2: - return new Int3(2, 0, -2); - case 3: - return new Int3(-2, 0, -2); + private boolean[] getSyphonableSlots(IInventory inventory, ForgeDirection direction) { + boolean[] result = new boolean[inventory.getSizeInventory()]; + if (inventory instanceof ISidedInventory sidedInv) { + int[] validSlots = sidedInv.getAccessibleSlotsFromSide(direction.ordinal()); + for (int slot : validSlots) result[slot] = true; + } else { + for (int i = 0; i < inventory.getSizeInventory(); i++) result[i] = true; } - return new Int3(0, 0, 0); + return result; + } + + private boolean canExtractItem(IInventory inventory, int slot, ItemStack stack, ForgeDirection direction) { + return stack != null && (!(inventory instanceof ISidedInventory) + || ((ISidedInventory) inventory).canExtractItem(slot, stack, direction.ordinal())); + } + + private ItemStack insertStackWithLimit(ItemStack stack, IInventory inventory, ForgeDirection direction, + int maxAmount) { + return maxAmount <= 0 ? SpellHelper.insertStackIntoInventory(stack, inventory, direction) + : SpellHelper.insertStackIntoInventory(stack, inventory, direction, maxAmount); + } + + private List findBufferChests(World world, int x, int y, int z, + Int3[] locations) { + List chestList = new ArrayList<>(); + for (Int3 chestLocation : locations) { + TileEntity tileEntity = world + .getTileEntity(x + chestLocation.xCoord, y + chestLocation.yCoord, z + chestLocation.zCoord); + if (tileEntity instanceof IInventory inv) { + chestList.add(inv); + } + } + return chestList; } @Override @@ -236,14 +171,14 @@ public class RitualEffectItemRouting extends RitualEffect { @Override public List getRitualComponentList() { - ArrayList omegaRitual = new ArrayList<>(); + ArrayList itemRoutingRitual = new ArrayList<>(); - this.addCornerRunes(omegaRitual, 1, 0, RitualComponent.BLANK); - this.addOffsetRunes(omegaRitual, 2, 1, 0, RitualComponent.FIRE); - this.addParallelRunes(omegaRitual, 4, 0, RitualComponent.WATER); - this.addParallelRunes(omegaRitual, 5, 0, RitualComponent.EARTH); - this.addCornerRunes(omegaRitual, 4, 0, RitualComponent.WATER); + this.addCornerRunes(itemRoutingRitual, 1, 0, RitualComponent.BLANK); + this.addOffsetRunes(itemRoutingRitual, 2, 1, 0, RitualComponent.FIRE); + this.addParallelRunes(itemRoutingRitual, 4, 0, RitualComponent.WATER); + this.addParallelRunes(itemRoutingRitual, 5, 0, RitualComponent.EARTH); + this.addCornerRunes(itemRoutingRitual, 4, 0, RitualComponent.WATER); - return omegaRitual; + return itemRoutingRitual; } }