Skip to content

Commit

Permalink
Fix some modules not being able to be reactivated with the mode chang…
Browse files Browse the repository at this point in the history
…e key. Also improve handling slightly for whether handle mode key gets forcibly disabled in cases like grav unit and jetpack
  • Loading branch information
pupnewfster committed Dec 10, 2021
1 parent 4294e43 commit 8269163
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 10 deletions.
12 changes: 12 additions & 0 deletions src/api/java/mekanism/api/gear/ICustomModule.java
Expand Up @@ -74,6 +74,18 @@ default void addHUDStrings(IModule<MODULE> module, Consumer<ITextComponent> hudS
default void addHUDElements(IModule<MODULE> module, Consumer<IHUDElement> hudElementAdder) {
}

/**
* Called to check if this module can change modes when disabled or if it should be skipped. This should be overridden for modules where the mode change key toggles
* whether the module is active.
*
* @param module Module instance.
*
* @return {@code true} if this module can change modes when disabled.
*/
default boolean canChangeModeWhenDisabled(IModule<MODULE> module) {
return false;
}

/**
* Called to change the mode of the module. This will only be called if {@link ModuleData#handlesModeChange()} is {@code true}. {@link
* IModule#displayModeChange(PlayerEntity, ITextComponent, IHasTextComponent)} is provided to help display the mode change when {@code displayChangeMessage} is {@code
Expand Down
2 changes: 1 addition & 1 deletion src/api/java/mekanism/api/gear/IModule.java
Expand Up @@ -43,7 +43,7 @@ public interface IModule<MODULE extends ICustomModule<MODULE>> {
boolean isEnabled();

/**
* Gets if this module type ({@link #getData()}) handles mode changes and if this module is configured to handle mode changes in the Module Tweaker.
* Gets if this module type ({@link #getData()}) can currently handle mode changes and if this module is configured to handle mode changes in the Module Tweaker.
*
* @return {@code true} if this module can handle mode changes.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/content/gear/Module.java
Expand Up @@ -278,7 +278,7 @@ public void changeMode(@Nonnull PlayerEntity player, @Nonnull ItemStack stack, i

@Override
public boolean handlesModeChange() {
return data.handlesModeChange() && handleModeChange.get();
return data.handlesModeChange() && handleModeChange.get() && (isEnabled() || customModule.canChangeModeWhenDisabled(this));
}

public void setModeHandlingDisabledForce() {
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/mekanism/common/content/gear/ModuleConfigItem.java
Expand Up @@ -49,14 +49,21 @@ public void set(@Nonnull TYPE val, @Nullable Runnable callback) {
data.set(val);
// validity checks
for (Module<?> m : ModuleHelper.INSTANCE.loadAll(module.getContainer())) {
// disable other exclusive modules
if (name.equals(Module.ENABLED_KEY) && val == Boolean.TRUE && module.getData().isExclusive()) {
if (m.getData().isExclusive() && m.getData() != module.getData()) {
m.setDisabledForce(callback != null);
boolean checkModeState;
if (name.equals(Module.ENABLED_KEY) && val == Boolean.TRUE) {
// disable other exclusive modules
if (module.getData().isExclusive()) {
if (m.getData().isExclusive() && m.getData() != module.getData()) {
m.setDisabledForce(callback != null);
}
}
//If enabled state of the module changes, recheck about mode changes
checkModeState = true;
} else {
checkModeState = name.equals(Module.HANDLE_MODE_CHANGE_KEY) && val == Boolean.TRUE;
}
// turn off mode change handling for other modules
if (name.equals(Module.HANDLE_MODE_CHANGE_KEY) && val == Boolean.TRUE && module.handlesModeChange()) {
if (checkModeState && module.handlesModeChange()) {
if (m.handlesModeChange() && m.getData() != module.getData()) {
m.setModeHandlingDisabledForce();
}
Expand Down
Expand Up @@ -38,6 +38,11 @@ public void addHUDElements(IModule<ModuleGravitationalModulatingUnit> module, Co
hudElementAdder.accept(MekanismAPI.getModuleHelper().hudElementEnabled(icon, module.isEnabled()));
}

@Override
public boolean canChangeModeWhenDisabled(IModule<ModuleGravitationalModulatingUnit> module) {
return true;
}

@Override
public void changeMode(IModule<ModuleGravitationalModulatingUnit> module, PlayerEntity player, ItemStack stack, int shift, boolean displayChangeMessage) {
module.toggleEnabled(player, MekanismLang.MODULE_GRAVITATIONAL_MODULATION.translate());
Expand Down
Expand Up @@ -76,6 +76,11 @@ private void pullItem(PlayerEntity player, ItemEntity item) {
player.position().add(0, 0.2, 0), item.position(), (int) (diff.length() * 4)), player);
}

@Override
public boolean canChangeModeWhenDisabled(IModule<ModuleMagneticAttractionUnit> module) {
return true;
}

@Override
public void changeMode(IModule<ModuleMagneticAttractionUnit> module, PlayerEntity player, ItemStack stack, int shift, boolean displayChangeMessage) {
module.toggleEnabled(player, MekanismLang.MODULE_MAGNETIC_ATTRACTION.translate());
Expand Down
Expand Up @@ -29,6 +29,11 @@ public void addHUDElements(IModule<ModuleVisionEnhancementUnit> module, Consumer
hudElementAdder.accept(MekanismAPI.getModuleHelper().hudElementEnabled(icon, module.isEnabled()));
}

@Override
public boolean canChangeModeWhenDisabled(IModule<ModuleVisionEnhancementUnit> module) {
return true;
}

@Override
public void changeMode(IModule<ModuleVisionEnhancementUnit> module, PlayerEntity player, ItemStack stack, int shift, boolean displayChangeMessage) {
module.toggleEnabled(player, MekanismLang.MODULE_VISION_ENHANCEMENT.translate());
Expand Down
Expand Up @@ -234,7 +234,7 @@ public void changeMode(@Nonnull PlayerEntity player, @Nonnull ItemStack stack, i

@Override
public boolean supportsSlotType(ItemStack stack, @Nonnull EquipmentSlotType slotType) {
return slotType == getSlot() && getModules(stack).stream().anyMatch(module -> module.isEnabled() && module.handlesModeChange());
return slotType == getSlot() && getModules(stack).stream().anyMatch(Module::handlesModeChange);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/mekanism/common/item/gear/ItemMekaTool.java
Expand Up @@ -344,7 +344,7 @@ public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantmen

@Override
public boolean supportsSlotType(ItemStack stack, @Nonnull EquipmentSlotType slotType) {
return IModeItem.super.supportsSlotType(stack, slotType) && getModules(stack).stream().anyMatch(module -> module.isEnabled() && module.handlesModeChange());
return IModeItem.super.supportsSlotType(stack, slotType) && getModules(stack).stream().anyMatch(Module::handlesModeChange);
}

@Override
Expand Down
Expand Up @@ -22,7 +22,7 @@ public PacketModeChange(EquipmentSlotType slot, int shift) {
this(slot, shift, false);
}

public PacketModeChange(EquipmentSlotType slot, int shift, boolean displayChangeMessage) {
private PacketModeChange(EquipmentSlotType slot, int shift, boolean displayChangeMessage) {
this.slot = slot;
this.shift = shift;
this.displayChangeMessage = displayChangeMessage;
Expand Down

0 comments on commit 8269163

Please sign in to comment.