Skip to content

Commit 81aba54

Browse files
authored
[Refactor] Abstract model index getter as IItemModelDispatcher (#2882)
1 parent dbe3a58 commit 81aba54

File tree

4 files changed

+59
-25
lines changed

4 files changed

+59
-25
lines changed

src/main/java/gregtech/api/items/metaitem/ElectricStats.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import java.util.List;
3333

3434
public class ElectricStats implements IItemComponent, IItemCapabilityProvider, IItemMaxStackSizeProvider,
35-
IItemBehaviour, ISubItemHandler {
35+
IItemBehaviour, ISubItemHandler, IItemModelDispatcher {
3636

3737
public static final ElectricStats EMPTY = new ElectricStats(0, 0, false, false);
3838

@@ -217,4 +217,14 @@ public static ElectricStats createRechargeableBattery(long maxCharge, int tier)
217217
public static ElectricStats createBattery(long maxCharge, int tier, boolean rechargeable) {
218218
return new ElectricStats(maxCharge, tier, rechargeable, true);
219219
}
220+
221+
@Override
222+
public int getModelIndex(ItemStack itemStack, int maxIndex) {
223+
IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
224+
if (electricItem != null) {
225+
return (int) Math.min(((electricItem.getCharge() / (electricItem.getMaxCharge() * 1.0)) * maxIndex),
226+
maxIndex);
227+
}
228+
return 0;
229+
}
220230
}

src/main/java/gregtech/api/items/metaitem/MetaItem.java

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,7 @@
1212
import gregtech.api.items.OreDictNames;
1313
import gregtech.api.items.gui.ItemUIFactory;
1414
import gregtech.api.items.gui.PlayerInventoryHolder;
15-
import gregtech.api.items.metaitem.stats.IEnchantabilityHelper;
16-
import gregtech.api.items.metaitem.stats.IFoodBehavior;
17-
import gregtech.api.items.metaitem.stats.IItemBehaviour;
18-
import gregtech.api.items.metaitem.stats.IItemCapabilityProvider;
19-
import gregtech.api.items.metaitem.stats.IItemColorProvider;
20-
import gregtech.api.items.metaitem.stats.IItemComponent;
21-
import gregtech.api.items.metaitem.stats.IItemContainerItemProvider;
22-
import gregtech.api.items.metaitem.stats.IItemDurabilityManager;
23-
import gregtech.api.items.metaitem.stats.IItemMaxStackSizeProvider;
24-
import gregtech.api.items.metaitem.stats.IItemNameProvider;
25-
import gregtech.api.items.metaitem.stats.IItemUseManager;
26-
import gregtech.api.items.metaitem.stats.ISubItemHandler;
27-
import gregtech.api.recipes.ingredients.IntCircuitIngredient;
15+
import gregtech.api.items.metaitem.stats.*;
2816
import gregtech.api.unification.OreDictUnifier;
2917
import gregtech.api.unification.material.Material;
3018
import gregtech.api.unification.ore.OrePrefix;
@@ -199,18 +187,17 @@ protected String formatModelPath(T metaValueItem) {
199187

200188
protected int getModelIndex(ItemStack itemStack) {
201189
T metaValueItem = getItem(itemStack);
190+
Objects.requireNonNull(metaValueItem);
202191

203-
// Electric Items
204-
IElectricItem electricItem = itemStack.getCapability(GregtechCapabilities.CAPABILITY_ELECTRIC_ITEM, null);
205-
if (electricItem != null) {
206-
return (int) Math.min(((electricItem.getCharge() / (electricItem.getMaxCharge() * 1.0)) * 7), 7);
207-
}
192+
var modelDispatcher = metaValueItem.getItemModelDispatcher();
193+
if (modelDispatcher == null) return 0;
208194

209-
// Integrated (Config) Circuit
210-
if (metaValueItem != null) {
211-
return IntCircuitIngredient.getCircuitConfiguration(itemStack);
212-
}
213-
return 0;
195+
int maxIndex = metaValueItem.getModelAmount() - 1;
196+
int index = modelDispatcher.getModelIndex(itemStack, maxIndex);
197+
Validate.inclusiveBetween(0, maxIndex, index,
198+
"Model index should be in range from 0 to %d (inclusive), where %d is supplied", maxIndex, index);
199+
200+
return index;
214201
}
215202

216203
@SideOnly(Side.CLIENT)
@@ -787,6 +774,7 @@ public MetaItem<T> getMetaItem() {
787774
private IItemColorProvider colorProvider;
788775
private IItemDurabilityManager durabilityManager;
789776
private IEnchantabilityHelper enchantabilityHelper;
777+
private IItemModelDispatcher itemModelDispatcher;
790778
private EnumRarity rarity;
791779

792780
private int burnValue = 0;
@@ -927,6 +915,9 @@ protected void addItemComponentsInternal(IItemComponent... stats) {
927915
if (itemComponent instanceof IEnchantabilityHelper) {
928916
this.enchantabilityHelper = (IEnchantabilityHelper) itemComponent;
929917
}
918+
if (itemComponent instanceof IItemModelDispatcher iItemModelDispatcher) {
919+
this.itemModelDispatcher = iItemModelDispatcher;
920+
}
930921
this.allStats.add(itemComponent);
931922
}
932923
}
@@ -987,6 +978,11 @@ public IEnchantabilityHelper getEnchantabilityHelper() {
987978
return enchantabilityHelper;
988979
}
989980

981+
@Nullable
982+
public IItemModelDispatcher getItemModelDispatcher() {
983+
return itemModelDispatcher;
984+
}
985+
990986
public int getBurnValue() {
991987
return burnValue;
992988
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package gregtech.api.items.metaitem.stats;
2+
3+
import gregtech.api.items.metaitem.MetaItem;
4+
5+
import net.minecraft.item.ItemStack;
6+
7+
@FunctionalInterface
8+
public interface IItemModelDispatcher extends IItemComponent {
9+
10+
/**
11+
* Get the model index for the given item stack. <br>
12+
* The index range will be checked at
13+
* {@link MetaItem#getModelIndex(ItemStack)}.
14+
*
15+
* @param itemStack The specific item stack.
16+
* @param maxIndex The max model index, from
17+
* {@link MetaItem.MetaValueItem#getModelAmount()} - 1.
18+
* @return The model index for the specific stack, should be ranged between
19+
* {@code 0} (inclusive) and {@code maxIndex} (inclusive).
20+
*/
21+
int getModelIndex(ItemStack itemStack, int maxIndex);
22+
}

src/main/java/gregtech/common/items/behaviors/IntCircuitBehaviour.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import gregtech.api.capability.IGhostSlotConfigurable;
44
import gregtech.api.items.gui.ItemUIFactory;
55
import gregtech.api.items.metaitem.stats.IItemBehaviour;
6+
import gregtech.api.items.metaitem.stats.IItemModelDispatcher;
67
import gregtech.api.items.metaitem.stats.ISubItemHandler;
78
import gregtech.api.metatileentity.MetaTileEntity;
89
import gregtech.api.mui.GTGuiTextures;
@@ -34,7 +35,7 @@
3435
import java.util.ArrayList;
3536
import java.util.List;
3637

37-
public class IntCircuitBehaviour implements IItemBehaviour, ItemUIFactory, ISubItemHandler {
38+
public class IntCircuitBehaviour implements IItemBehaviour, ItemUIFactory, ISubItemHandler, IItemModelDispatcher {
3839

3940
@Override
4041
public void addInformation(ItemStack itemStack, List<String> lines) {
@@ -116,4 +117,9 @@ public String getItemSubType(ItemStack itemStack) {
116117
public void getSubItems(ItemStack itemStack, CreativeTabs creativeTab, NonNullList<ItemStack> subItems) {
117118
subItems.add(itemStack.copy());
118119
}
120+
121+
@Override
122+
public int getModelIndex(ItemStack itemStack, int maxIndex) {
123+
return IntCircuitIngredient.getCircuitConfiguration(itemStack);
124+
}
119125
}

0 commit comments

Comments
 (0)