Skip to content

Commit

Permalink
remove/deprecate optionals from meka module system
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Apr 3, 2024
1 parent e1fe2bc commit 3ea5fd8
Show file tree
Hide file tree
Showing 25 changed files with 187 additions and 160 deletions.
46 changes: 35 additions & 11 deletions src/api/java/mekanism/api/gear/IModuleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public interface IModuleHelper {
* @return {@code true} if the item has the module installed and enabled.
*/
default boolean isEnabled(ItemStack stack, IModuleDataProvider<?> typeProvider) {
IModuleContainer container = getModuleContainer(stack).orElse(null);
IModuleContainer container = getModuleContainerNullable(stack);
return container != null && container.hasEnabled(typeProvider);
}

Expand All @@ -114,7 +114,7 @@ default boolean isEnabled(ItemStack stack, IModuleDataProvider<?> typeProvider)
*/
@Nullable
default <MODULE extends ICustomModule<MODULE>> IModule<MODULE> load(ItemStack stack, IModuleDataProvider<MODULE> typeProvider) {
IModuleContainer container = getModuleContainer(stack).orElse(null);
IModuleContainer container = getModuleContainerNullable(stack);
return container == null ? null : container.get(typeProvider);
}

Expand All @@ -125,7 +125,15 @@ default <MODULE extends ICustomModule<MODULE>> IModule<MODULE> load(ItemStack st
*
* @since 10.5.0
*/
Optional<? extends IModuleContainer> getModuleContainer(ItemStack stack);
@Deprecated(forRemoval = true)
default Optional<? extends IModuleContainer> getModuleContainer(ItemStack stack) {
return Optional.ofNullable(getModuleContainerNullable(stack));
}

@Nullable
IModuleContainer getModuleContainerNullable(ItemStack stack);

@Nullable <MODULE extends ICustomModule<MODULE>> IModule<MODULE> getModule(ItemStack stack, IModuleDataProvider<MODULE> typeProvider);

/**
* {@return module container for the item in entity's equipment slot, or empty if it is empty or not a module container}
Expand All @@ -135,11 +143,17 @@ default <MODULE extends ICustomModule<MODULE>> IModule<MODULE> load(ItemStack st
*
* @since 10.5.0
*/
@Deprecated(forRemoval = true)
default Optional<? extends IModuleContainer> getModuleContainer(@Nullable LivingEntity entity, @Nullable EquipmentSlot slot) {
return Optional.ofNullable(getModuleContainerNullable(entity, slot));
}

@Nullable
default IModuleContainer getModuleContainerNullable(@Nullable LivingEntity entity, @Nullable EquipmentSlot slot) {
if (entity == null || slot == null) {
return Optional.empty();
return null;
}
return getModuleContainer(entity.getItemBySlot(slot));
return getModuleContainerNullable(entity.getItemBySlot(slot));
}

/**
Expand Down Expand Up @@ -172,9 +186,8 @@ default boolean isModuleContainer(ItemStack stack) {
* @param stack Module container, for example a Meka-Tool or MekaSuit piece.
*/
default Collection<? extends IModule<?>> loadAll(ItemStack stack) {
return getModuleContainer(stack)
.map(IModuleContainer::modules)
.orElse(List.of());
IModuleContainer container = getModuleContainerNullable(stack);
return container != null ? container.modules() : List.of();
}

/**
Expand Down Expand Up @@ -204,9 +217,8 @@ default <MODULE extends ICustomModule<?>> List<? extends IModule<? extends MODUL
* @return Module types on an item.
*/
default Set<ModuleData<?>> loadAllTypes(ItemStack stack) {
return getModuleContainer(stack)
.map(IModuleContainer::moduleTypes)
.orElse(Set.of());
IModuleContainer container = getModuleContainerNullable(stack);
return container != null ? container.moduleTypes() : Set.of();
}

/**
Expand Down Expand Up @@ -277,4 +289,16 @@ default void addMekaSuitModuleModelSpec(String name, IModuleDataProvider<?> modu
* @apiNote Must only be called on the client side and from {@link FMLClientSetupEvent}.
*/
void addMekaSuitModuleModelSpec(String name, IModuleDataProvider<?> moduleDataProvider, EquipmentSlot slotType, Predicate<LivingEntity> isActive);

@Nullable
default <MODULE extends ICustomModule<MODULE>> IModule<MODULE> getIfEnabled(ItemStack stack, IModuleDataProvider<MODULE> typeProvider) {
IModuleContainer container = getModuleContainerNullable(stack);
return container == null ? null : container.getIfEnabled(typeProvider);
}

@Nullable
default <MODULE extends ICustomModule<MODULE>> IModule<MODULE> getIfEnabled(@Nullable LivingEntity entity, @Nullable EquipmentSlot slot, IModuleDataProvider<MODULE> typeProvider) {
IModuleContainer container = getModuleContainerNullable(entity, slot);
return container == null ? null : container.getIfEnabled(typeProvider);
}
}
14 changes: 9 additions & 5 deletions src/main/java/mekanism/client/ClientRegistration.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import mekanism.api.gear.IModule;
import mekanism.api.gear.IModuleContainer;
import mekanism.api.gear.IModuleHelper;
import mekanism.api.providers.IItemProvider;
import mekanism.api.text.EnumColor;
Expand Down Expand Up @@ -148,6 +150,7 @@
import mekanism.common.Mekanism;
import mekanism.common.attachments.FormulaAttachment;
import mekanism.common.block.attribute.Attribute;
import mekanism.common.content.gear.shared.ModuleColorModulationUnit;
import mekanism.common.item.ItemConfigurationCard;
import mekanism.common.item.block.machine.ItemBlockFluidTank;
import mekanism.common.lib.FieldReflectionHelper;
Expand Down Expand Up @@ -551,11 +554,12 @@ public static void registerItemColorHandlers(RegisterColorHandlersEvent.Item eve

ClientRegistrationUtil.registerItemColorHandler(event, (stack, index) -> {
if (index == 1) {
return IModuleHelper.INSTANCE.getModuleContainer(stack)
.map(container -> container.get(MekanismModules.COLOR_MODULATION_UNIT))
.map(colorModulation -> colorModulation.getCustomInstance().getColor())
.map(color -> color.toTint().argb())
.orElse(-1);
IModule<ModuleColorModulationUnit> colorModulationUnit = IModuleHelper.INSTANCE.getModule(stack, MekanismModules.COLOR_MODULATION_UNIT);
if (colorModulationUnit == null) {
return -1;
} else {
return colorModulationUnit.getCustomInstance().getColor().toTint().argb();//todo store tint argb on the unit?
}
}
return -1;
}, MekanismItems.MEKASUIT_HELMET, MekanismItems.MEKASUIT_BODYARMOR, MekanismItems.MEKASUIT_PANTS, MekanismItems.MEKASUIT_BOOTS);
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/mekanism/client/ClientTickHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public static boolean isGravitationalModulationOn(Player player) {
}

public static boolean isVisionEnhancementOn(Player player) {
IModuleContainer container = IModuleHelper.INSTANCE.getModuleContainer(player, EquipmentSlot.HEAD).orElse(null);
IModuleContainer container = IModuleHelper.INSTANCE.getModuleContainerNullable(player, EquipmentSlot.HEAD);
if (container != null && !container.isContainerOnCooldown(player)) {
IModule<ModuleVisionEnhancementUnit> module = container.getIfEnabled(MekanismModules.VISION_ENHANCEMENT_UNIT);
return module != null && module.hasEnoughEnergy(MekanismConfig.gear.mekaSuitEnergyUsageVisionEnhancement);
Expand Down Expand Up @@ -306,9 +306,8 @@ public void onFogLighting(ViewportEvent.ComputeFogColor event) {
@SubscribeEvent
public void onFog(ViewportEvent.RenderFog event) {
if (visionEnhancement && event.getCamera().getEntity() instanceof Player player) {
IModule<ModuleVisionEnhancementUnit> module = IModuleHelper.INSTANCE.getModuleContainer(player, EquipmentSlot.HEAD)
.map(container -> container.getIfEnabled(MekanismModules.VISION_ENHANCEMENT_UNIT))
.orElse(null);
IModuleContainer container = IModuleHelper.INSTANCE.getModuleContainerNullable(player, EquipmentSlot.HEAD);
IModule<ModuleVisionEnhancementUnit> module = container != null ? container.getIfEnabled(MekanismModules.VISION_ENHANCEMENT_UNIT) : null;
if (module != null) {
//This near plane is the same as spectators have set for lava and powdered snow
event.setNearPlaneDistance(-8.0F);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package mekanism.client.gui.element.custom.module;

import java.util.Optional;
import java.util.function.Consumer;
import mekanism.api.gear.IModule;
import mekanism.api.gear.config.IModuleConfigItem;
import mekanism.api.gear.config.ModuleColorData;
import mekanism.client.gui.GuiModuleTweaker;
import mekanism.client.gui.GuiUtils;
import mekanism.client.gui.element.scroll.GuiScrollList;
import mekanism.client.gui.element.text.GuiTextField;
import mekanism.client.gui.element.window.GuiColorWindow;
import mekanism.common.MekanismLang;
import mekanism.common.content.gear.Module;
import mekanism.common.content.gear.ModuleConfigItem;
import mekanism.common.content.gear.ModuleHelper;
import mekanism.common.content.gear.shared.ModuleColorModulationUnit;
Expand Down Expand Up @@ -84,20 +83,18 @@ protected void click(double mouseX, double mouseY) {
if (armorPreview != null && data.matches(MekanismModules.COLOR_MODULATION_UNIT, ModuleColorModulationUnit.COLOR_CONFIG_KEY) && currentModule != null) {
ItemStack stack = currentModule.getContainer().getPreviewStack();
if (stack.getItem() instanceof ArmorItem armorItem) {
Optional<IModuleConfigItem<Integer>> foundConfig = ModuleHelper.get().getModuleContainer(stack)
.map(container -> container.get(MekanismModules.COLOR_MODULATION_UNIT))
.map(module -> module.getConfigItem(ModuleColorModulationUnit.COLOR_CONFIG_KEY))
.filter(configItem -> configItem.getData() instanceof ModuleColorData)
.map(configItem -> (IModuleConfigItem<Integer>) configItem);
if (foundConfig.isPresent()) {
IModuleConfigItem<Integer> configItem = foundConfig.get();
//Ensure the preview has been initialized
armorPreview.get();
EquipmentSlot slot = armorItem.getEquipmentSlot();
//Replace the current preview with our copy
armorPreview.updatePreview(slot, stack);
updatePreviewColor = c -> configItem.set(c.argb());
previewReset = () -> armorPreview.resetToDefault(slot);
Module<ModuleColorModulationUnit> module = ModuleHelper.get().getModule(stack, MekanismModules.COLOR_MODULATION_UNIT);
if (module != null) {
ModuleColorData configItem = module.getConfigItemData(ModuleColorModulationUnit.COLOR_CONFIG_KEY, ModuleColorData.class);
if (configItem != null) {
//Ensure the preview has been initialized
armorPreview.get();
EquipmentSlot slot = armorItem.getEquipmentSlot();
//Replace the current preview with our copy
armorPreview.updatePreview(slot, stack);
updatePreviewColor = c -> configItem.set(c.argb());
previewReset = () -> armorPreview.resetToDefault(slot);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public GuiModuleScrollList(IGuiWrapper gui, int x, int y, int width, int height,

public void updateItemAndList(ItemStack stack) {
currentItem = stack;
currentContainer = ModuleHelper.get().getModuleContainer(stack).orElse(null);
currentContainer = ModuleHelper.get().getModuleContainerNullable(stack);
currentList.clear();
if (currentContainer != null) {
currentList.addAll(currentContainer.moduleTypes());
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/mekanism/client/render/HUDRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ private void renderMekaSuitModuleIcons(Player player, Font font, GuiGraphics gui
pose.pushPose();
//Render any elements that might be on modules in the meka suit while worn or on the meka tool while held
for (EquipmentSlot type : EQUIPMENT_ORDER) {
Optional<? extends IModuleContainer> moduleContainer = IModuleHelper.INSTANCE.getModuleContainer(player, type);
if (moduleContainer.isPresent()) {
for (IHUDElement element : moduleContainer.get().getHUDElements(player)) {
IModuleContainer moduleContainer = IModuleHelper.INSTANCE.getModuleContainerNullable(player, type);
if (moduleContainer != null) {
for (IHUDElement element : moduleContainer.getHUDElements(player)) {
curY -= 18;
if (reverseHud) {
//Align the mekasuit module icons to the left of the screen
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/mekanism/client/render/armor/MekaSuitArmor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import mekanism.api.gear.IModule;
import mekanism.api.gear.IModuleContainer;
import mekanism.api.gear.IModuleHelper;
import mekanism.api.gear.ModuleData;
Expand All @@ -40,6 +41,7 @@
import mekanism.client.render.lib.QuickHash;
import mekanism.client.render.lib.effect.BoltRenderer;
import mekanism.common.Mekanism;
import mekanism.common.content.gear.shared.ModuleColorModulationUnit;
import mekanism.common.item.gear.ItemMekaSuitArmor;
import mekanism.common.item.gear.ItemMekaTool;
import mekanism.common.lib.Color;
Expand Down Expand Up @@ -112,10 +114,8 @@ private MekaSuitArmor(EquipmentSlot type, EquipmentSlot adjacentType) {
}

private static Color getColor(ItemStack stack) {
return IModuleHelper.INSTANCE.getModuleContainer(stack)
.map(container -> container.get(MekanismModules.COLOR_MODULATION_UNIT))
.map(module -> module.getCustomInstance().getColor())
.orElse(Color.WHITE);
IModule<ModuleColorModulationUnit> colorUnit = IModuleHelper.INSTANCE.getModule(stack, MekanismModules.COLOR_MODULATION_UNIT);
return colorUnit != null ? colorUnit.getCustomInstance().getColor() : Color.WHITE;
}

public void renderArm(HumanoidModel<? extends LivingEntity> baseModel, @NotNull PoseStack matrix, @NotNull MultiBufferSource renderer, int light, int overlayLight,
Expand Down Expand Up @@ -540,10 +540,8 @@ public QuickHash key(LivingEntity player) {
Object2BooleanMap<ModuleModelSpec> modules = new Object2BooleanOpenHashMap<>();
Set<EquipmentSlot> wornParts = EnumSet.noneOf(EquipmentSlot.class);
for (EquipmentSlot slotType : EnumUtils.ARMOR_SLOTS) {
Optional<? extends IModuleContainer> optionalIModuleContainer = IModuleHelper.INSTANCE.getModuleContainer(player, slotType)
.filter(container -> container.isInstance(ItemMekaSuitArmor.class));
if (optionalIModuleContainer.isPresent()) {
IModuleContainer container = optionalIModuleContainer.get();
IModuleContainer container = IModuleHelper.INSTANCE.getModuleContainerNullable(player, slotType);
if (container != null && container.isInstance(ItemMekaSuitArmor.class)) {
wornParts.add(slotType);
for (Entry<ModuleData<?>, ModuleModelSpec> entry : moduleModelSpec.row(slotType).entrySet()) {
if (container.hasEnabled(entry.getKey())) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/mekanism/client/render/hud/MekanismHUD.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import mekanism.api.gear.IModuleContainer;
import mekanism.api.gear.IModuleHelper;
import mekanism.client.gui.GuiUtils;
import mekanism.client.render.HUDRenderer;
Expand Down Expand Up @@ -38,11 +39,11 @@ private MekanismHUD() {
@Nullable
private IItemHUDProvider getHudProvider(ItemStack stack) {
if (stack.getItem() instanceof IItemHUDProvider hudProvider) {
//mekanism does this
return hudProvider;
}
return IModuleHelper.INSTANCE.getModuleContainer(stack)
.<IItemHUDProvider>map(container -> (list, player, s, slotType) -> list.addAll(container.getHUDStrings(player)))
.orElse(null);
IModuleContainer container = IModuleHelper.INSTANCE.getModuleContainerNullable(stack);
return container != null ? (list, player, s, slotType) -> list.addAll(container.getHUDStrings(player)) : null;
}

@Override
Expand Down

0 comments on commit 3ea5fd8

Please sign in to comment.