Removed reliance on PartType from code outside of Bootstrapping,

and restored ability to group different supported items on
Upgrade Card tooltips.
This commit is contained in:
Sebastian Hartte
2020-07-01 00:33:15 +02:00
parent 400fcdb633
commit c67dba2f13
18 changed files with 368 additions and 596 deletions
@@ -18,12 +18,7 @@
package appeng.items.materials;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.Entity;
@@ -37,7 +32,6 @@ import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent;
import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
@@ -45,7 +39,6 @@ import net.minecraftforge.items.IItemHandler;
import appeng.api.config.Upgrades;
import appeng.api.implementations.IUpgradeableHost;
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageComponent;
import appeng.api.implementations.items.IUpgradeModule;
import appeng.api.implementations.tiles.ISegmentedInventory;
@@ -86,34 +79,7 @@ public final class MaterialItem extends AEBaseItem implements IStorageComponent,
final Upgrades u = this.getType(stack);
if (u != null) {
final List<ITextComponent> textList = new ArrayList<>();
for (final Entry<ItemStack, Integer> j : u.getSupported().entrySet()) {
ITextComponent name = null;
final int limit = j.getValue();
if (j.getKey().getItem() instanceof IItemGroup) {
final IItemGroup ig = (IItemGroup) j.getKey().getItem();
final String str = ig.getUnlocalizedGroupName(u.getSupported().keySet(), j.getKey());
if (str != null) {
name = new TranslationTextComponent(str).appendText(limit > 1 ? " (" + limit + ')' : "");
}
}
if (name == null) {
name = j.getKey().getDisplayName().appendText((limit > 1 ? " (" + limit + ')' : ""));
}
if (!textList.contains(name)) {
textList.add(name);
}
}
final Pattern p = Pattern.compile("(\\d+)[^\\d]");
// FIXME This comparison is not great...
final SlightlyBetterSort s = new SlightlyBetterSort(p);
textList.sort(s);
lines.addAll(textList);
lines.addAll(u.getTooltipLines());
}
}
@@ -229,27 +195,4 @@ public final class MaterialItem extends AEBaseItem implements IStorageComponent,
return false;
}
private static class SlightlyBetterSort implements Comparator<ITextComponent> {
private final Pattern pattern;
public SlightlyBetterSort(final Pattern pattern) {
this.pattern = pattern;
}
@Override
public int compare(final ITextComponent o1, final ITextComponent o2) {
try {
final Matcher a = this.pattern.matcher(o1.getString());
final Matcher b = this.pattern.matcher(o2.getString());
if (a.find() && b.find()) {
final int ia = Integer.parseInt(a.group(1));
final int ib = Integer.parseInt(b.group(1));
return Integer.compare(ia, ib);
}
} catch (final Throwable t) {
// ek!
}
return o1.getString().compareTo(o2.getString());
}
}
}
@@ -11,8 +11,8 @@ public class ColoredPartItem<T extends IPart> extends PartItem<T> {
private final AEColor color;
public ColoredPartItem(Properties properties, PartType type, Function<ItemStack, T> factory, AEColor color) {
super(properties, type, factory);
public ColoredPartItem(Properties properties, Function<ItemStack, T> factory, AEColor color) {
super(properties, factory);
this.color = color;
}
+2 -75
View File
@@ -18,31 +18,24 @@
package appeng.items.parts;
import java.util.Set;
import java.util.function.Function;
import javax.annotation.Nullable;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import appeng.api.AEApi;
import appeng.api.implementations.items.IItemGroup;
import appeng.api.parts.IPart;
import appeng.api.parts.IPartItem;
import appeng.core.localization.GuiText;
import appeng.items.AEBaseItem;
public class PartItem<T extends IPart> extends AEBaseItem implements IPartItem<T>, IItemGroup {
private final PartType type;
public class PartItem<T extends IPart> extends AEBaseItem implements IPartItem<T> {
private final Function<ItemStack, T> factory;
public PartItem(Properties properties, PartType type, Function<ItemStack, T> factory) {
public PartItem(Properties properties, Function<ItemStack, T> factory) {
super(properties);
this.type = type;
this.factory = factory;
}
@@ -58,75 +51,9 @@ public class PartItem<T extends IPart> extends AEBaseItem implements IPartItem<T
context.getHand(), context.getWorld());
}
public PartType getType() {
return type;
}
@Override
public T createPart(ItemStack is) {
return factory.apply(is);
}
private static PartType getTypeByStack(ItemStack is) {
if (is.getItem() instanceof PartItem) {
return ((PartItem<?>) is.getItem()).getType();
}
return PartType.INVALID_TYPE;
}
@Nullable
@Override
public String getUnlocalizedGroupName(final Set<ItemStack> others, final ItemStack is) {
boolean importBus = false;
boolean importBusFluids = false;
boolean exportBus = false;
boolean exportBusFluids = false;
boolean group = false;
final PartType u = getTypeByStack(is);
for (final ItemStack stack : others) {
if (stack.getItem() == this) {
final PartType pt = getTypeByStack(stack);
switch (pt) {
case IMPORT_BUS:
importBus = true;
if (u == pt) {
group = true;
}
break;
case FLUID_IMPORT_BUS:
importBusFluids = true;
if (u == pt) {
group = true;
}
break;
case EXPORT_BUS:
exportBus = true;
if (u == pt) {
group = true;
}
break;
case FLUID_EXPORT_BUS:
exportBusFluids = true;
if (u == pt) {
group = true;
}
break;
default:
}
}
}
if (group && importBus && exportBus && (u == PartType.IMPORT_BUS || u == PartType.EXPORT_BUS)) {
return GuiText.IOBuses.getTranslationKey();
}
if (group && importBusFluids && exportBusFluids
&& (u == PartType.FLUID_IMPORT_BUS || u == PartType.FLUID_EXPORT_BUS)) {
return GuiText.IOBusesFluids.getTranslationKey();
}
return null;
}
}
@@ -18,9 +18,9 @@ import appeng.core.AELog;
* Helps with the reflection magic needed to gather all models for AE2 cable bus
* parts.
*/
class PartModelsHelper {
public class PartModelsHelper {
static List<ResourceLocation> createModels(Class<?> clazz) {
public static List<ResourceLocation> createModels(Class<?> clazz) {
List<ResourceLocation> locations = new ArrayList<>();
// Check all static fields for used models
@@ -78,7 +78,7 @@ class PartModelsHelper {
continue;
}
Object value = null;
Object value;
try {
method.setAccessible(true);
value = method.invoke(null);
@@ -108,7 +108,7 @@ class PartModelsHelper {
locations.addAll(((IPartModel) value).getModels());
} else if (value instanceof Collection) {
// Check that each object is an IPartModel
Collection values = (Collection) value;
Collection<?> values = (Collection<?>) value;
for (Object candidate : values) {
if (!(candidate instanceof IPartModel)) {
AELog.error("List of locations obtained from {} contains a non resource location: {}", source,
@@ -1,243 +0,0 @@
/*
* This file is part of Applied Energistics 2.
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
*
* Applied Energistics 2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Applied Energistics 2 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
*/
package appeng.items.parts;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import net.minecraft.util.ResourceLocation;
import appeng.api.features.AEFeature;
import appeng.api.parts.IPart;
import appeng.fluids.parts.FluidAnnihilationPlanePart;
import appeng.fluids.parts.FluidExportBusPart;
import appeng.fluids.parts.FluidFormationPlanePart;
import appeng.fluids.parts.FluidImportBusPart;
import appeng.fluids.parts.FluidInterfacePart;
import appeng.fluids.parts.FluidLevelEmitterPart;
import appeng.fluids.parts.FluidStorageBusPart;
import appeng.fluids.parts.FluidTerminalPart;
import appeng.parts.automation.AnnihilationPlanePart;
import appeng.parts.automation.ExportBusPart;
import appeng.parts.automation.FormationPlanePart;
import appeng.parts.automation.IdentityAnnihilationPlanePart;
import appeng.parts.automation.ImportBusPart;
import appeng.parts.automation.LevelEmitterPart;
import appeng.parts.misc.CableAnchorPart;
import appeng.parts.misc.InterfacePart;
import appeng.parts.misc.InvertedToggleBusPart;
import appeng.parts.misc.StorageBusPart;
import appeng.parts.misc.ToggleBusPart;
import appeng.parts.networking.CoveredCablePart;
import appeng.parts.networking.CoveredDenseCablePart;
import appeng.parts.networking.GlassCablePart;
import appeng.parts.networking.QuartzFiberPart;
import appeng.parts.networking.SmartCablePart;
import appeng.parts.networking.SmartDenseCablePart;
import appeng.parts.p2p.FEP2PTunnelPart;
import appeng.parts.p2p.FluidP2PTunnelPart;
import appeng.parts.p2p.ItemP2PTunnelPart;
import appeng.parts.p2p.LightP2PTunnelPart;
import appeng.parts.p2p.MEP2PTunnelPart;
import appeng.parts.p2p.RedstoneP2PTunnelPart;
import appeng.parts.reporting.ConversionMonitorPart;
import appeng.parts.reporting.CraftingTerminalPart;
import appeng.parts.reporting.DarkPanelPart;
import appeng.parts.reporting.InterfaceTerminalPart;
import appeng.parts.reporting.PanelPart;
import appeng.parts.reporting.PatternTerminalPart;
import appeng.parts.reporting.SemiDarkPanelPart;
import appeng.parts.reporting.StorageMonitorPart;
import appeng.parts.reporting.TerminalPart;
public enum PartType {
INVALID_TYPE(EnumSet.of(AEFeature.CORE), EnumSet.noneOf(IntegrationType.class), null),
CABLE_GLASS(EnumSet.of(AEFeature.GLASS_CABLES), EnumSet.noneOf(IntegrationType.class), GlassCablePart.class) {
@Override
public boolean isCable() {
return true;
}
},
CABLE_COVERED(EnumSet.of(AEFeature.COVERED_CABLES), EnumSet.noneOf(IntegrationType.class), CoveredCablePart.class) {
@Override
public boolean isCable() {
return true;
}
},
CABLE_SMART(EnumSet.of(AEFeature.CHANNELS, AEFeature.SMART_CABLES), EnumSet.noneOf(IntegrationType.class),
SmartCablePart.class) {
@Override
public boolean isCable() {
return true;
}
},
CABLE_DENSE_SMART(EnumSet.of(AEFeature.CHANNELS, AEFeature.DENSE_CABLES), EnumSet.noneOf(IntegrationType.class),
SmartDenseCablePart.class) {
@Override
public boolean isCable() {
return true;
}
},
CABLE_DENSE_COVERED(EnumSet.of(AEFeature.CHANNELS, AEFeature.DENSE_CABLES), EnumSet.noneOf(IntegrationType.class),
CoveredDenseCablePart.class) {
@Override
public boolean isCable() {
return true;
}
},
TOGGLE_BUS(EnumSet.of(AEFeature.TOGGLE_BUS), EnumSet.noneOf(IntegrationType.class), ToggleBusPart.class),
INVERTED_TOGGLE_BUS(EnumSet.of(AEFeature.TOGGLE_BUS), EnumSet.noneOf(IntegrationType.class),
InvertedToggleBusPart.class),
CABLE_ANCHOR(EnumSet.of(AEFeature.CABLE_ANCHOR), EnumSet.noneOf(IntegrationType.class), CableAnchorPart.class),
QUARTZ_FIBER(EnumSet.of(AEFeature.QUARTZ_FIBER), EnumSet.noneOf(IntegrationType.class), QuartzFiberPart.class),
MONITOR(EnumSet.of(AEFeature.PANELS), EnumSet.noneOf(IntegrationType.class), PanelPart.class),
SEMI_DARK_MONITOR(EnumSet.of(AEFeature.PANELS), EnumSet.noneOf(IntegrationType.class), SemiDarkPanelPart.class),
DARK_MONITOR(EnumSet.of(AEFeature.PANELS), EnumSet.noneOf(IntegrationType.class), DarkPanelPart.class),
STORAGE_BUS(EnumSet.of(AEFeature.STORAGE_BUS), EnumSet.noneOf(IntegrationType.class), StorageBusPart.class),
FLUID_STORAGE_BUS(EnumSet.of(AEFeature.FLUID_STORAGE_BUS), EnumSet.noneOf(IntegrationType.class),
FluidStorageBusPart.class),
IMPORT_BUS(EnumSet.of(AEFeature.IMPORT_BUS), EnumSet.noneOf(IntegrationType.class), ImportBusPart.class),
FLUID_IMPORT_BUS(EnumSet.of(AEFeature.FLUID_IMPORT_BUS), EnumSet.noneOf(IntegrationType.class),
FluidImportBusPart.class),
EXPORT_BUS(EnumSet.of(AEFeature.EXPORT_BUS), EnumSet.noneOf(IntegrationType.class), ExportBusPart.class),
FLUID_EXPORT_BUS(EnumSet.of(AEFeature.FLUID_EXPORT_BUS), EnumSet.noneOf(IntegrationType.class),
FluidExportBusPart.class),
LEVEL_EMITTER(EnumSet.of(AEFeature.LEVEL_EMITTER), EnumSet.noneOf(IntegrationType.class), LevelEmitterPart.class),
FLUID_LEVEL_EMITTER(EnumSet.of(AEFeature.FLUID_LEVEL_EMITTER), EnumSet.noneOf(IntegrationType.class),
FluidLevelEmitterPart.class),
ANNIHILATION_PLANE(EnumSet.of(AEFeature.ANNIHILATION_PLANE), EnumSet.noneOf(IntegrationType.class),
AnnihilationPlanePart.class),
IDENTITY_ANNIHILATION_PLANE(EnumSet.of(AEFeature.ANNIHILATION_PLANE, AEFeature.IDENTITY_ANNIHILATION_PLANE),
EnumSet.noneOf(IntegrationType.class), IdentityAnnihilationPlanePart.class),
FLUID_ANNIHILATION_PLANE(EnumSet.of(AEFeature.FLUID_ANNIHILATION_PLANE), EnumSet.noneOf(IntegrationType.class),
FluidAnnihilationPlanePart.class),
FORMATION_PLANE(EnumSet.of(AEFeature.FORMATION_PLANE), EnumSet.noneOf(IntegrationType.class),
FormationPlanePart.class),
FLUID_FORMATION_PLANE(EnumSet.of(AEFeature.FLUID_FORMATION_PLANE), EnumSet.noneOf(IntegrationType.class),
FluidFormationPlanePart.class),
PATTERN_TERMINAL(EnumSet.of(AEFeature.PATTERNS), EnumSet.noneOf(IntegrationType.class), PatternTerminalPart.class),
CRAFTING_TERMINAL(EnumSet.of(AEFeature.CRAFTING_TERMINAL), EnumSet.noneOf(IntegrationType.class),
CraftingTerminalPart.class),
TERMINAL(EnumSet.of(AEFeature.TERMINAL), EnumSet.noneOf(IntegrationType.class), TerminalPart.class),
STORAGE_MONITOR(EnumSet.of(AEFeature.STORAGE_MONITOR), EnumSet.noneOf(IntegrationType.class),
StorageMonitorPart.class),
CONVERSION_MONITOR(EnumSet.of(AEFeature.PART_CONVERSION_MONITOR), EnumSet.noneOf(IntegrationType.class),
ConversionMonitorPart.class),
INTERFACE(EnumSet.of(AEFeature.INTERFACE), EnumSet.noneOf(IntegrationType.class), InterfacePart.class),
FLUID_INTERFACE(EnumSet.of(AEFeature.FLUID_INTERFACE), EnumSet.noneOf(IntegrationType.class),
FluidInterfacePart.class),
P2P_TUNNEL_ME(EnumSet.of(AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_ME), EnumSet.noneOf(IntegrationType.class),
MEP2PTunnelPart.class),
P2P_TUNNEL_REDSTONE(EnumSet.of(AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_REDSTONE),
EnumSet.noneOf(IntegrationType.class), RedstoneP2PTunnelPart.class),
P2P_TUNNEL_ITEM(EnumSet.of(AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_ITEMS), EnumSet.noneOf(IntegrationType.class),
ItemP2PTunnelPart.class),
P2P_TUNNEL_FLUID(EnumSet.of(AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_FLUIDS),
EnumSet.noneOf(IntegrationType.class), FluidP2PTunnelPart.class),
//FIXME P2P_TUNNEL_IC2( 465, "p2p_tunnel_ic2", EnumSet.of( AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_EU ), EnumSet
//FIXME .of( IntegrationType.IC2 ), PartP2PIC2Power.class ),
P2P_TUNNEL_LIGHT(EnumSet.of(AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_LIGHT),
EnumSet.noneOf(IntegrationType.class), LightP2PTunnelPart.class),
P2P_TUNNEL_FE(EnumSet.of(AEFeature.P2P_TUNNEL, AEFeature.P2P_TUNNEL_FE), EnumSet.noneOf(IntegrationType.class),
FEP2PTunnelPart.class),
// P2PTunnelOpenComputers( 468, EnumSet.of( AEFeature.P2PTunnel,
// AEFeature.P2PTunnelOpenComputers ), EnumSet.of(
// IntegrationType.OpenComputers ), PartP2POpenComputers.class, GuiText.OCTunnel
// ),
INTERFACE_TERMINAL(EnumSet.of(AEFeature.INTERFACE_TERMINAL), EnumSet.noneOf(IntegrationType.class),
InterfaceTerminalPart.class),
FLUID_TERMINAL(EnumSet.of(AEFeature.FLUID_TERMINAL), EnumSet.noneOf(IntegrationType.class),
FluidTerminalPart.class);
private final Set<AEFeature> features;
private final Set<IntegrationType> integrations;
private final Set<ResourceLocation> models;
PartType(final Set<AEFeature> features, final Set<IntegrationType> integrations, final Class<? extends IPart> c) {
this.features = Collections.unmodifiableSet(features);
this.integrations = Collections.unmodifiableSet(integrations);
if (c != null) {
this.models = new HashSet<>(PartModelsHelper.createModels(c));
} else {
this.models = Collections.emptySet();
}
}
public boolean isCable() {
return false;
}
public Set<AEFeature> getFeature() {
return this.features;
}
Set<IntegrationType> getIntegrations() {
return this.integrations;
}
public Set<ResourceLocation> getModels() {
return this.models;
}
}
enum IntegrationType {
}
@@ -39,7 +39,6 @@ import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.exceptions.MissingDefinitionException;
import appeng.api.features.AEFeature;
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageCell;
import appeng.api.implementations.items.IUpgradeModule;
import appeng.api.storage.IMEInventoryHandler;
@@ -60,8 +59,7 @@ import appeng.util.Platform;
* @version rv6 - 2018-01-17
* @since rv6 2018-01-17
*/
public abstract class AbstractStorageCell<T extends IAEStack<T>> extends AEBaseItem
implements IStorageCell<T>, IItemGroup {
public abstract class AbstractStorageCell<T extends IAEStack<T>> extends AEBaseItem implements IStorageCell<T> {
protected final MaterialType component;
protected final int totalBytes;
@@ -104,11 +102,6 @@ public abstract class AbstractStorageCell<T extends IAEStack<T>> extends AEBaseI
return true;
}
@Override
public String getUnlocalizedGroupName(final Set<ItemStack> others, final ItemStack is) {
return GuiText.StorageCells.getTranslationKey();
}
@Override
public boolean isEditable(final ItemStack is) {
return true;
@@ -52,7 +52,6 @@ import net.minecraftforge.items.IItemHandler;
import appeng.api.AEApi;
import appeng.api.config.Actionable;
import appeng.api.config.FuzzyMode;
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageCell;
import appeng.api.implementations.tiles.IColorableTile;
import appeng.api.storage.IMEInventory;
@@ -80,7 +79,7 @@ import appeng.util.Platform;
import appeng.util.item.AEItemStack;
public class ColorApplicatorItem extends AEBasePoweredItem
implements IStorageCell<IAEItemStack>, IItemGroup, IBlockTool, IMouseWheelItem {
implements IStorageCell<IAEItemStack>, IBlockTool, IMouseWheelItem {
private static final Map<ResourceLocation, AEColor> TAG_TO_COLOR = ImmutableMap.<ResourceLocation, AEColor>builder()
.put(new ResourceLocation("forge:dyes/black"), AEColor.BLACK)
@@ -409,11 +408,6 @@ public class ColorApplicatorItem extends AEBasePoweredItem
return AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
}
@Override
public String getUnlocalizedGroupName(final Set<ItemStack> others, final ItemStack is) {
return GuiText.StorageCells.getTranslationKey();
}
@Override
public boolean isEditable(final ItemStack is) {
return true;
@@ -39,7 +39,6 @@ import appeng.api.AEApi;
import appeng.api.config.FuzzyMode;
import appeng.api.implementations.guiobjects.IGuiItem;
import appeng.api.implementations.guiobjects.IGuiItemObject;
import appeng.api.implementations.items.IItemGroup;
import appeng.api.implementations.items.IStorageCell;
import appeng.api.storage.IStorageChannel;
import appeng.api.storage.cells.ICellInventoryHandler;
@@ -55,7 +54,7 @@ import appeng.items.contents.CellUpgrades;
import appeng.items.contents.PortableCellViewer;
import appeng.items.tools.powered.powersink.AEBasePoweredItem;
public class PortableCellItem extends AEBasePoweredItem implements IStorageCell<IAEItemStack>, IGuiItem, IItemGroup {
public class PortableCellItem extends AEBasePoweredItem implements IStorageCell<IAEItemStack>, IGuiItem {
public PortableCellItem(Item.Properties props) {
super(AEConfig.instance().getPortableCellBattery(), props);
}
@@ -118,11 +117,6 @@ public class PortableCellItem extends AEBasePoweredItem implements IStorageCell<
return AEApi.instance().storage().getStorageChannel(IItemStorageChannel.class);
}
@Override
public String getUnlocalizedGroupName(final Set<ItemStack> others, final ItemStack is) {
return GuiText.StorageCells.getTranslationKey();
}
@Override
public boolean isEditable(final ItemStack is) {
return true;