From 3db2d4960b385f1e5ffa6ffd58a03a1ff96aa590 Mon Sep 17 00:00:00 2001 From: thatsIch Date: Mon, 29 Sep 2014 09:23:02 +0200 Subject: [PATCH] Replaced normal for loops with foreach loops which is less error prone when just iterating over collection/array --- .../appeng/container/AEBaseContainer.java | 22 +++++++++------ .../ContainerCellWorkbench.java | 4 +-- .../ContainerMEMonitorable.java | 4 +-- .../implementations/ContainerPatternTerm.java | 8 +++--- .../implementations/ContainerSecurity.java | 4 +-- .../appeng/helpers/MultiCraftingTracker.java | 6 +++-- .../NEIHelpers/NEICraftingHandler.java | 7 +++-- .../appeng/items/misc/ItemEncodedPattern.java | 16 ++++++----- .../implementations/CraftingCPUCluster.java | 8 +++--- .../java/appeng/recipes/handlers/Shaped.java | 8 ++++-- .../appeng/recipes/handlers/Shapeless.java | 13 ++++----- src/main/java/appeng/util/Platform.java | 4 +-- .../util/inv/AdaptorISpecialInventory.java | 9 ++++--- .../java/appeng/util/inv/AdaptorList.java | 27 ++++++++++--------- .../util/inv/WrapperInventoryRange.java | 6 +++-- 15 files changed, 86 insertions(+), 60 deletions(-) diff --git a/src/main/java/appeng/container/AEBaseContainer.java b/src/main/java/appeng/container/AEBaseContainer.java index 088a4724..51fba512 100644 --- a/src/main/java/appeng/container/AEBaseContainer.java +++ b/src/main/java/appeng/container/AEBaseContainer.java @@ -360,28 +360,32 @@ public abstract class AEBaseContainer extends Container tis = shiftStoreItem( tis ); // target slots in the container... - for (int x = 0; x < this.inventorySlots.size(); x++) + for (Object inventorySlot : this.inventorySlots) { - AppEngSlot cs = (AppEngSlot) this.inventorySlots.get( x ); + AppEngSlot cs = (AppEngSlot) inventorySlot; if ( !(cs.isPlayerSide()) && !(cs instanceof SlotFake) && !(cs instanceof SlotCraftingMatrix) ) { if ( cs.isItemValid( tis ) ) + { selectedSlots.add( cs ); + } } } } else { // target slots in the container... - for (int x = 0; x < this.inventorySlots.size(); x++) + for (Object inventorySlot : this.inventorySlots) { - AppEngSlot cs = (AppEngSlot) this.inventorySlots.get( x ); + AppEngSlot cs = (AppEngSlot) inventorySlot; if ( (cs.isPlayerSide()) && !(cs instanceof SlotFake) && !(cs instanceof SlotCraftingMatrix) ) { if ( cs.isItemValid( tis ) ) + { selectedSlots.add( cs ); + } } } } @@ -394,15 +398,17 @@ public abstract class AEBaseContainer extends Container if ( tis != null ) { // target slots in the container... - for (int x = 0; x < this.inventorySlots.size(); x++) + for (Object inventorySlot : this.inventorySlots) { - AppEngSlot cs = (AppEngSlot) this.inventorySlots.get( x ); + AppEngSlot cs = (AppEngSlot) inventorySlot; ItemStack dest = cs.getStack(); if ( !(cs.isPlayerSide()) && cs instanceof SlotFake ) { if ( Platform.isSameItemPrecise( dest, tis ) ) + { return null; + } else if ( dest == null ) { cs.putStack( tis.copy() ); @@ -563,9 +569,9 @@ public abstract class AEBaseContainer extends Container if ( Platform.isServer() ) { - for (int i = 0; i < this.crafters.size(); ++i) + for (Object crafter : this.crafters) { - ICrafting icrafting = (ICrafting) this.crafters.get( i ); + ICrafting icrafting = (ICrafting) crafter; for (SyncData sd : syncData.values()) sd.tick( icrafting ); diff --git a/src/main/java/appeng/container/implementations/ContainerCellWorkbench.java b/src/main/java/appeng/container/implementations/ContainerCellWorkbench.java index cc4f18c0..6a0c002f 100644 --- a/src/main/java/appeng/container/implementations/ContainerCellWorkbench.java +++ b/src/main/java/appeng/container/implementations/ContainerCellWorkbench.java @@ -242,9 +242,9 @@ public class ContainerCellWorkbench extends ContainerUpgradeable ItemStack is = workBench.getInventoryByName( "cell" ).getStackInSlot( 0 ); if ( Platform.isServer() ) { - for (int i = 0; i < this.crafters.size(); ++i) + for (Object crafter : this.crafters) { - ICrafting icrafting = (ICrafting) this.crafters.get( i ); + ICrafting icrafting = (ICrafting) crafter; if ( prevStack != is ) { diff --git a/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java b/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java index d33e51f4..26fa72c4 100644 --- a/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java +++ b/src/main/java/appeng/container/implementations/ContainerMEMonitorable.java @@ -152,11 +152,11 @@ public class ContainerMEMonitorable extends AEBaseContainer implements IConfigMa if ( sideLocal != sideRemote ) { clientCM.putSetting( set, sideLocal ); - for (int j = 0; j < this.crafters.size(); ++j) + for (Object crafter : this.crafters) { try { - NetworkHandler.instance.sendTo( new PacketValueConfig( set.name(), sideLocal.name() ), (EntityPlayerMP) this.crafters.get( j ) ); + NetworkHandler.instance.sendTo( new PacketValueConfig( set.name(), sideLocal.name() ), (EntityPlayerMP) crafter ); } catch (IOException e) { diff --git a/src/main/java/appeng/container/implementations/ContainerPatternTerm.java b/src/main/java/appeng/container/implementations/ContainerPatternTerm.java index 140f81ec..c6538eab 100644 --- a/src/main/java/appeng/container/implementations/ContainerPatternTerm.java +++ b/src/main/java/appeng/container/implementations/ContainerPatternTerm.java @@ -262,9 +262,9 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA List list = new ArrayList( 3 ); boolean hasValue = false; - for (int x = 0; x < outputSlots.length; x++) + for (OptionalSlotFake outputSlot : outputSlots) { - ItemStack out = outputSlots[x].getStack(); + ItemStack out = outputSlot.getStack(); if ( out != null && out.stackSize > 0 ) { list.add( out ); @@ -395,9 +395,9 @@ public class ContainerPatternTerm extends ContainerMEMonitorable implements IAEA { if ( s == patternSlotOUT && Platform.isServer() ) { - for (int i = 0; i < this.crafters.size(); ++i) + for (Object crafter : this.crafters) { - ICrafting icrafting = (ICrafting) this.crafters.get( i ); + ICrafting icrafting = (ICrafting) crafter; for (Object g : inventorySlots) { diff --git a/src/main/java/appeng/container/implementations/ContainerSecurity.java b/src/main/java/appeng/container/implementations/ContainerSecurity.java index 2a99a832..056aef58 100644 --- a/src/main/java/appeng/container/implementations/ContainerSecurity.java +++ b/src/main/java/appeng/container/implementations/ContainerSecurity.java @@ -134,9 +134,9 @@ public class ContainerSecurity extends ContainerMEMonitorable implements IAEAppE wirelessOut.putStack( term ); // update the two slots in question... - for (int i = 0; i < this.crafters.size(); ++i) + for (Object crafter : this.crafters) { - ICrafting icrafting = (ICrafting) this.crafters.get( i ); + ICrafting icrafting = (ICrafting) crafter; icrafting.sendSlotContents( this, wirelessIn.slotNumber, wirelessIn.getStack() ); icrafting.sendSlotContents( this, wirelessOut.slotNumber, wirelessOut.getStack() ); } diff --git a/src/main/java/appeng/helpers/MultiCraftingTracker.java b/src/main/java/appeng/helpers/MultiCraftingTracker.java index 2d56c256..96be0c40 100644 --- a/src/main/java/appeng/helpers/MultiCraftingTracker.java +++ b/src/main/java/appeng/helpers/MultiCraftingTracker.java @@ -150,10 +150,12 @@ public class MultiCraftingTracker jobs[slot] = l; boolean hasStuff = false; - for (int x = 0; x < jobs.length; x++) + for (Future job : jobs) { - if ( jobs[x] != null ) + if ( job != null ) + { hasStuff = true; + } } if ( hasStuff == false ) diff --git a/src/main/java/appeng/integration/modules/NEIHelpers/NEICraftingHandler.java b/src/main/java/appeng/integration/modules/NEIHelpers/NEICraftingHandler.java index 20840e79..72fe4703 100644 --- a/src/main/java/appeng/integration/modules/NEIHelpers/NEICraftingHandler.java +++ b/src/main/java/appeng/integration/modules/NEIHelpers/NEICraftingHandler.java @@ -55,9 +55,8 @@ public class NEICraftingHandler implements IOverlayHandler if ( gui instanceof GuiCraftingTerm || gui instanceof GuiPatternTerm ) { - for (int i = 0; i < ingredients.size(); i++)// identify slots + for (PositionedStack positionedStack : ingredients) { - PositionedStack positionedStack = ingredients.get( i ); int col = (positionedStack.relx - 25) / 18; int row = (positionedStack.rely - 6) / 18; if ( positionedStack.items != null && positionedStack.items.length > 0 ) @@ -76,9 +75,13 @@ public class NEICraftingHandler implements IOverlayHandler for (int x = 0; x < positionedStack.items.length; x++) { if ( Platform.isRecipePrioritized( positionedStack.items[x] ) ) + { list.add( 0, positionedStack.items[x] ); + } else + { list.add( positionedStack.items[x] ); + } } for (ItemStack is : list) diff --git a/src/main/java/appeng/items/misc/ItemEncodedPattern.java b/src/main/java/appeng/items/misc/ItemEncodedPattern.java index 3fd8c91c..c9898a16 100644 --- a/src/main/java/appeng/items/misc/ItemEncodedPattern.java +++ b/src/main/java/appeng/items/misc/ItemEncodedPattern.java @@ -89,22 +89,26 @@ public class ItemEncodedPattern extends AEBaseItem implements ICraftingPatternIt String with = GuiText.With.getLocal() + ": "; boolean first = true; - for (int x = 0; x < out.length; x++) + for (IAEItemStack anOut : out) { - if ( out[x] == null ) + if ( anOut == null ) + { continue; + } - l.add( (first ? label : and) + out[x].getStackSize() + " " + Platform.getItemDisplayName( out[x] ) ); + l.add( (first ? label : and) + anOut.getStackSize() + " " + Platform.getItemDisplayName( anOut ) ); first = false; } first = true; - for (int x = 0; x < in.length; x++) + for (IAEItemStack anIn : in) { - if ( in[x] == null ) + if ( anIn == null ) + { continue; + } - l.add( (first ? with : and) + in[x].getStackSize() + " " + Platform.getItemDisplayName( in[x] ) ); + l.add( (first ? with : and) + anIn.getStackSize() + " " + Platform.getItemDisplayName( anIn ) ); first = false; } } diff --git a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java index a3bf7bd3..a93060d6 100644 --- a/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java +++ b/src/main/java/appeng/me/cluster/implementations/CraftingCPUCluster.java @@ -595,10 +595,12 @@ public class CraftingCPUCluster implements IAECluster, ICraftingCPU IAEItemStack[] input = details.getInputs(); double sum = 0; - for (int x = 0; x < input.length; x++) + for (IAEItemStack anInput : input) { - if ( input[x] != null ) - sum += input[x].getStackSize(); + if ( anInput != null ) + { + sum += anInput.getStackSize(); + } } // power... diff --git a/src/main/java/appeng/recipes/handlers/Shaped.java b/src/main/java/appeng/recipes/handlers/Shaped.java index c4c2dd3b..9cbd8828 100644 --- a/src/main/java/appeng/recipes/handlers/Shaped.java +++ b/src/main/java/appeng/recipes/handlers/Shaped.java @@ -35,9 +35,13 @@ public class Shaped implements ICraftHandler, IWebsiteSerializer cols = input.get( 0 ).size(); if ( cols <= 3 && cols >= 1 ) { - for (int x = 0; x < input.size(); x++) - if ( input.get( x ).size() != cols ) + for (List anInput : input) + { + if ( anInput.size() != cols ) + { throw new RecipeError( "all rows in a shaped crafting recipe must contain the same number of ingredients." ); + } + } inputs = input; this.output = output.get( 0 ).get( 0 ); diff --git a/src/main/java/appeng/recipes/handlers/Shapeless.java b/src/main/java/appeng/recipes/handlers/Shapeless.java index 5f41c808..4e3eea58 100644 --- a/src/main/java/appeng/recipes/handlers/Shapeless.java +++ b/src/main/java/appeng/recipes/handlers/Shapeless.java @@ -61,18 +61,19 @@ public class Shapeless implements ICraftHandler, IWebsiteSerializer @Override public boolean canCraft(ItemStack reqOutput) throws RegistrationError, MissingIngredientError { - for ( int y = 0; y < inputs.size(); y++ ) + for (IIngredient i : inputs) { - IIngredient i = inputs.get(y); - if ( !i.isAir() ) + if ( !i.isAir() ) + { + for (ItemStack r : i.getItemStackSet()) { - for ( ItemStack r : i.getItemStackSet() ) + if ( Platform.isSameItemPrecise( r, reqOutput ) ) { - if ( Platform.isSameItemPrecise( r, reqOutput) ) - return false; + return false; } } } + } return Platform.isSameItemPrecise( output.getItemStack(),reqOutput ); } diff --git a/src/main/java/appeng/util/Platform.java b/src/main/java/appeng/util/Platform.java index 6de0d6bc..20af0c5d 100644 --- a/src/main/java/appeng/util/Platform.java +++ b/src/main/java/appeng/util/Platform.java @@ -575,10 +575,8 @@ public class Platform CraftingManager cm = CraftingManager.getInstance(); List rl = cm.getRecipeList(); - for (int x = 0; x < rl.size(); ++x) + for (IRecipe r : rl) { - IRecipe r = rl.get( x ); - if ( r.matches( par1InventoryCrafting, par2World ) ) { return r; diff --git a/src/main/java/appeng/util/inv/AdaptorISpecialInventory.java b/src/main/java/appeng/util/inv/AdaptorISpecialInventory.java index d09b0b50..9bc3f0b3 100644 --- a/src/main/java/appeng/util/inv/AdaptorISpecialInventory.java +++ b/src/main/java/appeng/util/inv/AdaptorISpecialInventory.java @@ -89,10 +89,13 @@ public class AdaptorISpecialInventory extends InventoryAdaptor if ( slots == null ) return false; - int s = slots.length; - for (int x = 0; x < s; x++) - if ( i.getStackInSlot( slots[x] ) != null ) + for (int slot : slots) + { + if ( i.getStackInSlot( slot ) != null ) + { return true; + } + } return false; } diff --git a/src/main/java/appeng/util/inv/AdaptorList.java b/src/main/java/appeng/util/inv/AdaptorList.java index 0f6f1664..2561dfc2 100644 --- a/src/main/java/appeng/util/inv/AdaptorList.java +++ b/src/main/java/appeng/util/inv/AdaptorList.java @@ -52,16 +52,18 @@ public class AdaptorList extends InventoryAdaptor @Override public ItemStack simulateSimilarRemove(int how_many, ItemStack filter, FuzzyMode fuzzyMode, IInventoryDestination dest) { - int s = i.size(); - for (int x = 0; x < s; x++) + for (ItemStack is : i) { - ItemStack is = i.get( x ); if ( is != null && (filter == null || Platform.isSameItemFuzzy( is, filter, fuzzyMode )) ) { if ( how_many > is.stackSize ) + { how_many = is.stackSize; + } if ( dest != null && !dest.canInsert( is ) ) + { how_many = 0; + } if ( how_many > 0 ) { @@ -109,16 +111,18 @@ public class AdaptorList extends InventoryAdaptor @Override public ItemStack simulateRemove(int how_many, ItemStack filter, IInventoryDestination dest) { - int s = i.size(); - for (int x = 0; x < s; x++) + for (ItemStack is : i) { - ItemStack is = i.get( x ); if ( is != null && (filter == null || Platform.isSameItemPrecise( is, filter )) ) { if ( how_many > is.stackSize ) + { how_many = is.stackSize; + } if ( dest != null && !dest.canInsert( is ) ) + { how_many = 0; + } if ( how_many > 0 ) { @@ -142,10 +146,8 @@ public class AdaptorList extends InventoryAdaptor ItemStack left = A.copy(); - int s = i.size(); - for (int x = 0; x < s; x++) + for (ItemStack is : i) { - ItemStack is = i.get( x ); if ( Platform.isSameItem( is, left ) ) { is.stackSize += left.stackSize; @@ -166,13 +168,12 @@ public class AdaptorList extends InventoryAdaptor @Override public boolean containsItems() { - - int s = i.size(); - for (int x = 0; x < s; x++) + for (ItemStack is : i) { - ItemStack is = i.get( x ); if ( is != null ) + { return true; + } } return false; } diff --git a/src/main/java/appeng/util/inv/WrapperInventoryRange.java b/src/main/java/appeng/util/inv/WrapperInventoryRange.java index 4a8b8325..99d99003 100644 --- a/src/main/java/appeng/util/inv/WrapperInventoryRange.java +++ b/src/main/java/appeng/util/inv/WrapperInventoryRange.java @@ -16,11 +16,13 @@ public class WrapperInventoryRange implements IInventory if ( s.length > 0 ) { StringBuilder sb = new StringBuilder(); - for (int i = 0; i < s.length; i++) + for (int value : s) { if ( sb.length() > 0 ) + { sb.append( separator ); - sb.append( s[i] ); + } + sb.append( value ); } return sb.toString(); }