From 567ca3286f758139493fbfb83a2953dca045511a Mon Sep 17 00:00:00 2001 From: mewacaser Date: Fri, 6 May 2022 13:31:55 -0500 Subject: [PATCH] MekaTool Radial Profiles Rebased, cleaned up, and squashed. Squashed commits: MekaTool radial profiles kinda working, more GUI work needed Added packet to save mekatool profile, save client config, gui fixes Small changes to the enum Added profile hud string and removed handle mode key from efficiency module --- src/api/java/mekanism/api/NBTConstants.java | 1 + src/datagen/generated/mekanism/.cache/cache | 4 +- .../mekanism/assets/mekanism/lang/en_ud.json | 2 + .../mekanism/assets/mekanism/lang/en_us.json | 2 + .../client/lang/MekanismLangProvider.java | 2 + .../mekanism/client/gui/GuiModuleTweaker.java | 11 +- .../gui/element/custom/GuiModuleScreen.java | 4 + .../element/window/GuiMekaToolOptions.java | 48 +++++++ .../java/mekanism/common/MekanismLang.java | 2 + .../mekanism/common/config/ClientConfig.java | 6 + .../container/SelectedWindowData.java | 1 + .../common/item/gear/ItemMekaTool.java | 135 ++++++++++++++++-- .../common/item/interfaces/ISaveModeItem.java | 14 ++ .../common/network/PacketHandler.java | 2 + .../network/to_server/PacketSaveMode.java | 40 ++++++ .../to_server/PacketUpdateModuleSettings.java | 4 + .../common/registries/MekanismModules.java | 2 +- 17 files changed, 261 insertions(+), 19 deletions(-) create mode 100644 src/main/java/mekanism/client/gui/element/window/GuiMekaToolOptions.java create mode 100644 src/main/java/mekanism/common/item/interfaces/ISaveModeItem.java create mode 100644 src/main/java/mekanism/common/network/to_server/PacketSaveMode.java diff --git a/src/api/java/mekanism/api/NBTConstants.java b/src/api/java/mekanism/api/NBTConstants.java index 9374515d8a0..363d693ce27 100644 --- a/src/api/java/mekanism/api/NBTConstants.java +++ b/src/api/java/mekanism/api/NBTConstants.java @@ -142,6 +142,7 @@ private NBTConstants() { public static final String MODE = "mode"; public static final String MODID = "modID"; public static final String MODULES = "modules"; + public static final String MODULE_PROFILE = "profile_"; public static final String NAME = "name"; public static final String NUM_POWERING = "numPowering"; public static final String ORIGINAL_LOCATION = "originalLocation"; diff --git a/src/datagen/generated/mekanism/.cache/cache b/src/datagen/generated/mekanism/.cache/cache index 3397e261787..fc444fad5c9 100644 --- a/src/datagen/generated/mekanism/.cache/cache +++ b/src/datagen/generated/mekanism/.cache/cache @@ -35,8 +35,8 @@ f5e3eb6799a15d0b72969547614dfc51dac53419 assets/mekanism/blockstates/tin_ore.jso 8b4570f0148089999c54bb9695f9c94346455c7a assets/mekanism/blockstates/uranium_hexafluoride.json 246c1904e7699b01a3a20f2a2b79b35b5d99aec9 assets/mekanism/blockstates/uranium_ore.json 40c623e2db3159e7355b25f422c719294eca4b64 assets/mekanism/blockstates/uranium_oxide.json -6f9d1d2150486ed8bef26fe415cec7404a0d26eb assets/mekanism/lang/en_ud.json -b95a15c4b873fb7c14be6ee6ab4fc51e494c353e assets/mekanism/lang/en_us.json +f417753043e29b880be2d6165e45dff971f24936 assets/mekanism/lang/en_ud.json +a7f6b4597cd9c86bc3352f5d4dfbcd15e4370137 assets/mekanism/lang/en_us.json beea3be5a8af802be66d456a7a648678bede0f02 assets/mekanism/models/block/brine.json beea3be5a8af802be66d456a7a648678bede0f02 assets/mekanism/models/block/chlorine.json dcf97575f9590e37cd289a662002e3bc903953a2 assets/mekanism/models/block/deepslate_ore/fluorite.json diff --git a/src/datagen/generated/mekanism/assets/mekanism/lang/en_ud.json b/src/datagen/generated/mekanism/assets/mekanism/lang/en_ud.json index d11e5ca588c..4f2f8b1e029 100644 --- a/src/datagen/generated/mekanism/assets/mekanism/lang/en_ud.json +++ b/src/datagen/generated/mekanism/assets/mekanism/lang/en_ud.json @@ -804,6 +804,7 @@ "gui.mekanism.liquid": "%s :p\u1D09nb\u1D09\uA780", "gui.mekanism.max": "%s :x\u0250W", "gui.mekanism.max_output": "\u0287/%s :\u0287nd\u0287nO x\u0250W", + "gui.mekanism.mekatool_options": "s\u01DD\uA781\u1D09\u025Fo\u0279\u0500 \uA781oo\u27D8\u0250\u029E\u01DDW", "gui.mekanism.min": "%s :u\u1D09W", "gui.mekanism.move_down": "u\u028Do\u15E1 \u01DD\u028CoW", "gui.mekanism.move_up": "d\u2229 \u01DD\u028CoW", @@ -1140,6 +1141,7 @@ "module.mekanism.mode_change": "%2$s :o\u0287 p\u01DDd\u026Fnq %1$s", "module.mekanism.module_tweaker": "\u0279\u01DD\u029E\u0250\u01DD\u028D\u27D8 \u01DD\uA781npoW", "module.mekanism.nutritional_injection_unit": "\u0287\u1D09u\u2229 uo\u1D09\u0287\u0254\u01DD\u027EuI \uA781\u0250uo\u1D09\u0287\u1D09\u0279\u0287nN", + "module.mekanism.profile_selected": "%s :\u01DD\uA781\u1D09\u025Fo\u0279\u0500", "module.mekanism.purification.beneficial": "\uA781\u0250\u1D09\u0254\u1D09\u025F\u01DDu\u01DD\u15FA \u01DD\u028Co\u026F\u01DD\u1D1A", "module.mekanism.purification.harmful": "\uA781n\u025F\u026F\u0279\u0250H \u01DD\u028Co\u026F\u01DD\u1D1A", "module.mekanism.purification.neutral": "\uA781\u0250\u0279\u0287n\u01DDN \u01DD\u028Co\u026F\u01DD\u1D1A", diff --git a/src/datagen/generated/mekanism/assets/mekanism/lang/en_us.json b/src/datagen/generated/mekanism/assets/mekanism/lang/en_us.json index 165762c314a..e3d93f1ab1b 100644 --- a/src/datagen/generated/mekanism/assets/mekanism/lang/en_us.json +++ b/src/datagen/generated/mekanism/assets/mekanism/lang/en_us.json @@ -804,6 +804,7 @@ "gui.mekanism.liquid": "Liquid: %1$s", "gui.mekanism.max": "Max: %1$s", "gui.mekanism.max_output": "Max Output: %1$s/t", + "gui.mekanism.mekatool_options": "MekaTool Profiles", "gui.mekanism.min": "Min: %1$s", "gui.mekanism.move_down": "Move Down", "gui.mekanism.move_up": "Move Up", @@ -1140,6 +1141,7 @@ "module.mekanism.mode_change": "%1$s bumped to: %2$s", "module.mekanism.module_tweaker": "Module Tweaker", "module.mekanism.nutritional_injection_unit": "Nutritional Injection Unit", + "module.mekanism.profile_selected": "Profile: %1$s", "module.mekanism.purification.beneficial": "Remove Beneficial", "module.mekanism.purification.harmful": "Remove Harmful", "module.mekanism.purification.neutral": "Remove Neutral", diff --git a/src/datagen/main/java/mekanism/client/lang/MekanismLangProvider.java b/src/datagen/main/java/mekanism/client/lang/MekanismLangProvider.java index 98e7e0c2727..f07ba3bfa04 100644 --- a/src/datagen/main/java/mekanism/client/lang/MekanismLangProvider.java +++ b/src/datagen/main/java/mekanism/client/lang/MekanismLangProvider.java @@ -715,6 +715,7 @@ private void addMisc() { add(MekanismLang.COLOR_PICKER, "Color Picker"); add(MekanismLang.HELMET_OPTIONS, "Helmet Options"); add(MekanismLang.HUD_OVERLAY, "HUD Overlay:"); + add(MekanismLang.MEKATOOL_OPTIONS, "MekaTool Profiles"); add(MekanismLang.OPACITY, "Opacity"); add(MekanismLang.DEFAULT, "Default"); add(MekanismLang.WARNING, "Warning"); @@ -1252,6 +1253,7 @@ private void addMisc() { add(MekanismLang.MODULE_HANDLE_MODE_CHANGE, "Handle Mode Key"); add(MekanismLang.MODULE_RENDER_HUD, "Show in HUD"); add(MekanismLang.MODULE_MODE, "Mode"); + add(MekanismLang.MODULE_PROFILE_SELECTED, "Profile: %1$s"); add(MekanismLang.MODULE_BONUS_ATTACK_DAMAGE, "Bonus Attack Damage"); add(MekanismLang.MODULE_FARMING_RADIUS, "Farming Radius"); add(MekanismLang.MODULE_JUMP_BOOST, "Jump Boost"); diff --git a/src/main/java/mekanism/client/gui/GuiModuleTweaker.java b/src/main/java/mekanism/client/gui/GuiModuleTweaker.java index cd2ee4e8a1a..a71eabf1b35 100644 --- a/src/main/java/mekanism/client/gui/GuiModuleTweaker.java +++ b/src/main/java/mekanism/client/gui/GuiModuleTweaker.java @@ -11,6 +11,7 @@ import mekanism.client.gui.element.slot.GuiSlot; import mekanism.client.gui.element.slot.SlotType; import mekanism.client.gui.element.window.GuiMekaSuitHelmetOptions; +import mekanism.client.gui.element.window.GuiMekaToolOptions; import mekanism.common.MekanismLang; import mekanism.common.content.gear.Module; import mekanism.common.inventory.container.ModuleTweakerContainer; @@ -19,6 +20,7 @@ import net.minecraft.network.chat.Component; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.inventory.Slot; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import org.lwjgl.glfw.GLFW; @@ -64,7 +66,12 @@ private void onModuleSelected(Module module) { } private void openOptions() { - addWindow(new GuiMekaSuitHelmetOptions(this, getWidth() / 2 - 140 / 2, getHeight() / 2 - 90 / 2)); + Item item = getStack(selected).getItem(); + if(item == MekanismItems.MEKASUIT_HELMET.get()) { + addWindow(new GuiMekaSuitHelmetOptions(this, getWidth() / 2 - 140 / 2, getHeight() / 2 - 90 / 2)); + } else if(item == MekanismItems.MEKA_TOOL.get()) { + addWindow(new GuiMekaToolOptions(this, getWidth() / 2 - 140 / 2, getHeight() / 2 - 90 / 2, menu.slots.get(selected).getSlotIndex())); + } } @Override @@ -121,7 +128,7 @@ private void select(int index) { selected = index; ItemStack stack = getStack(index); scrollList.updateList(stack, true); - optionsButton.active = stack.getItem() == MekanismItems.MEKASUIT_HELMET.get(); + optionsButton.active = (stack.getItem() == MekanismItems.MEKASUIT_HELMET.get()) || (stack.getItem() == MekanismItems.MEKA_TOOL.get()); } } diff --git a/src/main/java/mekanism/client/gui/element/custom/GuiModuleScreen.java b/src/main/java/mekanism/client/gui/element/custom/GuiModuleScreen.java index db2a7cc0a71..25cebc92e24 100644 --- a/src/main/java/mekanism/client/gui/element/custom/GuiModuleScreen.java +++ b/src/main/java/mekanism/client/gui/element/custom/GuiModuleScreen.java @@ -48,6 +48,10 @@ private Runnable getCallback(ModuleConfigData configData, int dataIndex) { return () -> { if (currentModule != null) {//Shouldn't be null but validate just in case Mekanism.packetHandler().sendToServer(PacketUpdateModuleSettings.create(slotIdSupplier.getAsInt(), currentModule.getData(), dataIndex, configData)); + // If we want to have it sync changes to current mode from the client we would put it here + // if (currentModule.getContainer().getItem() instanceof ISaveModeItem saveModeItem /* TODO && MekanismConfig.client.mekaModeSync.get() */) { + // Mekanism.packetHandler().sendToServer(new PacketSaveMode(slotIdSupplier.getAsInt(), ItemDataUtils.getInt(currentModule.getContainer(), NBTConstants.MODE))); + // } } }; } diff --git a/src/main/java/mekanism/client/gui/element/window/GuiMekaToolOptions.java b/src/main/java/mekanism/client/gui/element/window/GuiMekaToolOptions.java new file mode 100644 index 00000000000..b08ece4ce8e --- /dev/null +++ b/src/main/java/mekanism/client/gui/element/window/GuiMekaToolOptions.java @@ -0,0 +1,48 @@ +package mekanism.client.gui.element.window; + +import com.mojang.blaze3d.vertex.PoseStack; +import mekanism.client.gui.IGuiWrapper; +import mekanism.client.gui.element.button.TranslationButton; +import mekanism.client.gui.element.text.GuiTextField; +import mekanism.common.Mekanism; +import mekanism.common.MekanismLang; +import mekanism.common.config.MekanismConfig; +import mekanism.common.inventory.container.SelectedWindowData.WindowType; +import mekanism.common.item.gear.ItemMekaTool.MekaToolMode; +import mekanism.common.network.to_server.PacketSaveMode; +import mekanism.common.util.text.InputValidator; + +public class GuiMekaToolOptions extends GuiWindow { + + private static final int HEIGHT = 12; + private static final int WIDTH = 64; + + public GuiMekaToolOptions(IGuiWrapper gui, int x, int y, int slotId) { + super(gui, x, y, 140, 115, WindowType.MEKA_TOOL_MODES); + interactionStrategy = InteractionStrategy.NONE; + + for (MekaToolMode mode : MekaToolMode.values()) { + GuiTextField profileName = new GuiTextField(gui, relativeX + 7, relativeY + 20 + HEIGHT * mode.ordinal(), WIDTH, HEIGHT); + profileName.setText(MekanismConfig.client.mekaModeNames.get().get(mode.ordinal())); + profileName.setMaxLength(10); + profileName.setInputValidator(InputValidator.LETTER_OR_DIGIT); + profileName.addCheckmarkButton(() -> { + if (!profileName.getText().isBlank()) { + MekanismConfig.client.mekaModeNames.get().set(mode.ordinal(), profileName.getText()); + // save the updated config info + MekanismConfig.client.save(); + } + }); + addChild(profileName); + addChild(new TranslationButton(gui, relativeX + 7 + WIDTH, relativeY + 20 + HEIGHT * mode.ordinal(), 32, HEIGHT, + MekanismLang.BUTTON_SAVE, () -> Mekanism.packetHandler().sendToServer(new PacketSaveMode(slotId, mode.ordinal())))); + } + } + + @Override + public void renderForeground(PoseStack matrix, int mouseX, int mouseY) { + super.renderForeground(matrix, mouseX, mouseY); + + drawTitleText(matrix, MekanismLang.MEKATOOL_OPTIONS.translate(), 6); + } +} diff --git a/src/main/java/mekanism/common/MekanismLang.java b/src/main/java/mekanism/common/MekanismLang.java index 20c25cd23fa..0fd2590e946 100644 --- a/src/main/java/mekanism/common/MekanismLang.java +++ b/src/main/java/mekanism/common/MekanismLang.java @@ -228,6 +228,7 @@ public enum MekanismLang implements ILangEntry { COLOR_PICKER("gui", "color_picker"), RGB("gui", "rgb"), HELMET_OPTIONS("gui", "helmet_options"), + MEKATOOL_OPTIONS("gui", "mekatool_options"), HUD_OVERLAY("gui", "hud_overlay"), OPACITY("gui", "opacity"), DEFAULT("gui", "default"), @@ -765,6 +766,7 @@ public enum MekanismLang implements ILangEntry { MODULE_HANDLE_MODE_CHANGE("module", "handle_mode_change"), MODULE_RENDER_HUD("module", "render_hud"), MODULE_MODE("module", "mode"), + MODULE_PROFILE_SELECTED("module", "profile_selected"), MODULE_BONUS_ATTACK_DAMAGE("module", "bonus_attack_damage"), MODULE_FARMING_RADIUS("module", "farming_radius"), MODULE_JUMP_BOOST("module", "jump_boost"), diff --git a/src/main/java/mekanism/common/config/ClientConfig.java b/src/main/java/mekanism/common/config/ClientConfig.java index a79ba051523..9fb84984aa6 100644 --- a/src/main/java/mekanism/common/config/ClientConfig.java +++ b/src/main/java/mekanism/common/config/ClientConfig.java @@ -1,8 +1,11 @@ package mekanism.common.config; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import mekanism.common.config.value.CachedBooleanValue; +import mekanism.common.config.value.CachedConfigValue; import mekanism.common.config.value.CachedEnumValue; import mekanism.common.config.value.CachedFloatValue; import mekanism.common.config.value.CachedIntValue; @@ -49,6 +52,7 @@ public class ClientConfig extends BaseMekanismConfig { public final CachedFloatValue hudJitter; public final CachedBooleanValue hudCompassEnabled; public final Map lastWindowPositions = new HashMap<>(); + public final CachedConfigValue> mekaModeNames; public final CachedEnumValue qioItemViewerSortType; public final CachedEnumValue qioItemViewerSortDirection; @@ -110,6 +114,8 @@ public class ClientConfig extends BaseMekanismConfig { .defineInRange("hudJitter", 6F, 1F, 100F)); hudCompassEnabled = CachedBooleanValue.wrap(this, builder.comment("Display a fancy compass when the MekaSuit is worn.") .define("mekaSuitHelmetCompass", true)); + mekaModeNames = CachedConfigValue.wrap(this, builder.comment("Meka Tool mode names.") + .define("mekaModeNames", new ArrayList<>(List.of("Mode1", "Mode2", "Mode3", "Mode4", "Mode5", "Mode6")))); builder.comment("Last Window Positions. In general these values should not be modified manually.").push(GUI_WINDOW_CATEGORY); for (WindowType windowType : WindowType.values()) { for (String savePath : windowType.getSavePaths()) { diff --git a/src/main/java/mekanism/common/inventory/container/SelectedWindowData.java b/src/main/java/mekanism/common/inventory/container/SelectedWindowData.java index 47a4bd68329..57b49cd89da 100644 --- a/src/main/java/mekanism/common/inventory/container/SelectedWindowData.java +++ b/src/main/java/mekanism/common/inventory/container/SelectedWindowData.java @@ -97,6 +97,7 @@ public enum WindowType { CONFIRMATION("confirmation"), CRAFTING("crafting", IQIOCraftingWindowHolder.MAX_CRAFTING_WINDOWS), MEKA_SUIT_HELMET("mekaSuitHelmet"), + MEKA_TOOL_MODES("mekaToolModes"), RENAME("rename"), SKIN_SELECT("skinSelect"), SIDE_CONFIG("sideConfig"), diff --git a/src/main/java/mekanism/common/item/gear/ItemMekaTool.java b/src/main/java/mekanism/common/item/gear/ItemMekaTool.java index 6525e075028..6a55c23d286 100644 --- a/src/main/java/mekanism/common/item/gear/ItemMekaTool.java +++ b/src/main/java/mekanism/common/item/gear/ItemMekaTool.java @@ -15,11 +15,18 @@ import javax.annotation.Nonnull; import mekanism.api.Action; import mekanism.api.AutomationType; +import mekanism.api.NBTConstants; import mekanism.api.energy.IEnergyContainer; import mekanism.api.gear.ICustomModule; import mekanism.api.gear.IModule; +import mekanism.api.gear.config.ModuleBooleanData; +import mekanism.api.gear.config.ModuleConfigData; +import mekanism.api.gear.config.ModuleEnumData; import mekanism.api.math.FloatingLong; +import mekanism.api.math.MathUtils; import mekanism.api.text.EnumColor; +import mekanism.api.text.IHasTextComponent; +import mekanism.api.text.TextComponentUtil; import mekanism.client.key.MekKeyHandler; import mekanism.client.key.MekanismKeyHandler; import mekanism.common.Mekanism; @@ -28,6 +35,8 @@ import mekanism.common.content.gear.IBlastingItem; import mekanism.common.content.gear.IModuleContainerItem; import mekanism.common.content.gear.Module; +import mekanism.common.content.gear.ModuleConfigItem; +import mekanism.common.content.gear.ModuleHelper; import mekanism.common.content.gear.mekatool.ModuleAttackAmplificationUnit; import mekanism.common.content.gear.mekatool.ModuleBlastingUnit; import mekanism.common.content.gear.mekatool.ModuleExcavationEscalationUnit; @@ -35,14 +44,20 @@ import mekanism.common.content.gear.mekatool.ModuleVeinMiningUnit; import mekanism.common.content.gear.shared.ModuleEnergyUnit; import mekanism.common.item.ItemEnergized; -import mekanism.common.item.interfaces.IModeItem; +import mekanism.common.item.gear.ItemMekaTool.MekaToolMode; +import mekanism.common.item.interfaces.IRadialModeItem; +import mekanism.common.item.interfaces.IRadialSelectorEnum; +import mekanism.common.item.interfaces.ISaveModeItem; import mekanism.common.network.to_client.PacketPortalFX; import mekanism.common.registries.MekanismModules; import mekanism.common.tags.MekanismTags; +import mekanism.common.util.ItemDataUtils; import mekanism.common.util.MekanismUtils; import mekanism.common.util.StorageUtils; import net.minecraft.core.BlockPos; +import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; @@ -74,7 +89,7 @@ import net.minecraftforge.common.ToolAction; import net.minecraftforge.fluids.IFluidBlock; -public class ItemMekaTool extends ItemEnergized implements IModuleContainerItem, IModeItem, IBlastingItem { +public class ItemMekaTool extends ItemEnergized implements IModuleContainerItem, IRadialModeItem, ISaveModeItem, IBlastingItem { private final Multimap attributes; @@ -378,18 +393,44 @@ public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantmen return false; } - @Override - public boolean supportsSlotType(ItemStack stack, @Nonnull EquipmentSlot slotType) { - return IModeItem.super.supportsSlotType(stack, slotType) && getModules(stack).stream().anyMatch(Module::handlesModeChange); - } + public enum MekaToolMode implements IRadialSelectorEnum, IHasTextComponent { + ONE(), + TWO(), + THREE(), + FOUR(), + FIVE(), + SIX(); - @Override - public void changeMode(@Nonnull Player player, @Nonnull ItemStack stack, int shift, boolean displayChangeMessage) { - for (Module module : getModules(stack)) { - if (module.handlesModeChange()) { - module.changeMode(player, stack, shift, displayChangeMessage); - return; - } + public String getKey() { + return NBTConstants.MODULE_PROFILE + ordinal(); + } + + public boolean isEnabled() { + return true; + } + + public static MekaToolMode byIndexStatic(int index) { + return MathUtils.getByIndexMod(values(), index); + } + + @Override + public @Nonnull MekaToolMode byIndex(int index) { + return byIndexStatic(index); + } + + @Override + public Component getTextComponent() { + return TextComponentUtil.getString(MekanismConfig.client.mekaModeNames.get().get(ordinal())); + } + + @Override + public Component getShortText() { + return getTextComponent(); + } + + @Override + public ResourceLocation getIcon() { + return MekanismUtils.getResource(MekanismUtils.ResourceType.GUI, "void.png"); } } @@ -404,4 +445,70 @@ protected FloatingLong getChargeRate(ItemStack stack) { IModule module = getModule(stack, MekanismModules.ENERGY_UNIT); return module == null ? MekanismConfig.gear.mekaToolBaseChargeRate.get() : module.getCustomInstance().getChargeRate(module); } -} \ No newline at end of file + + @Override + public MekaToolMode getMode(ItemStack itemStack) { + return MekaToolMode.byIndexStatic(ItemDataUtils.getInt(itemStack, NBTConstants.MODE)); + } + + @Override + public MekaToolMode getModeByIndex(int ordinal) { + return MekaToolMode.byIndexStatic(ordinal); + } + + @Override + public void setMode(ItemStack stack, Player player, MekaToolMode mode) { + ItemDataUtils.setInt(stack, NBTConstants.MODE, mode.ordinal()); + + CompoundTag modeTag = ItemDataUtils.getCompound(stack, mode.getKey()); + for (Module m : ModuleHelper.INSTANCE.loadAll(stack)) { + CompoundTag modeNBT = modeTag.getCompound(m.getData().getRegistryName().toString()); + for (ModuleConfigItem item : m.getConfigItems()) { + if (modeNBT.contains(item.getName())) { + setValue(item, modeNBT); + } + } + } + } + + @SuppressWarnings("unchecked") + private void setValue(ModuleConfigItem moduleConfigItem, CompoundTag valueContainer) { + ModuleConfigData configData = moduleConfigItem.getData(); + if (configData instanceof ModuleBooleanData) { + ((ModuleConfigItem) moduleConfigItem).set(valueContainer.getBoolean(moduleConfigItem.getName())); + } else if (configData instanceof ModuleEnumData) { + moduleConfigItem.set((TYPE) MathUtils.getByIndexMod(((ModuleEnumData) configData).getEnums(), valueContainer.getInt(moduleConfigItem.getName()))); + } + } + + @Override + public Class getModeClass() { + return MekaToolMode.class; + } + + @Override + public void changeMode(@Nonnull Player player, @Nonnull ItemStack stack, int shift, boolean displayChangeMessage) { + setMode(stack, player, shift == 0 ? getMode(stack).getPrevious() : getMode(stack).getNext()); + } + + @Override + public void saveMode(@Nonnull ItemStack stack, int modeId) { + MekaToolMode mode = MekaToolMode.byIndexStatic(modeId); + CompoundTag modeTag = new CompoundTag(); + for (Module m : ModuleHelper.INSTANCE.loadAll(stack)) { + CompoundTag modeNBT = new CompoundTag(); + for (ModuleConfigItem item : m.getConfigItems()) { + item.write(modeNBT); + } + modeTag.put(m.getData().getRegistryName().toString(), modeNBT); + } + ItemDataUtils.setCompound(stack, mode.getKey(), modeTag); + } + + @Override + public void addHUDStrings(List list, Player player, ItemStack stack, EquipmentSlot slotType) { + IModuleContainerItem.super.addHUDStrings(list, player, stack, slotType); + + list.add(MekanismLang.MODULE_PROFILE_SELECTED.translateColored(EnumColor.DARK_GRAY, EnumColor.INDIGO, getMode(stack).getShortText())); + } +} diff --git a/src/main/java/mekanism/common/item/interfaces/ISaveModeItem.java b/src/main/java/mekanism/common/item/interfaces/ISaveModeItem.java new file mode 100644 index 00000000000..c82ea9d89e1 --- /dev/null +++ b/src/main/java/mekanism/common/item/interfaces/ISaveModeItem.java @@ -0,0 +1,14 @@ +package mekanism.common.item.interfaces; + +import javax.annotation.Nonnull; +import net.minecraft.world.item.ItemStack; + +public interface ISaveModeItem { + + /** + * Saves the current state to a profile of the item + * @param stack The stack to change the mode of + * @param modeId The mode to save to + */ + void saveMode(@Nonnull ItemStack stack, int modeId); +} \ No newline at end of file diff --git a/src/main/java/mekanism/common/network/PacketHandler.java b/src/main/java/mekanism/common/network/PacketHandler.java index 37577f25ebf..fddd9d20b15 100644 --- a/src/main/java/mekanism/common/network/PacketHandler.java +++ b/src/main/java/mekanism/common/network/PacketHandler.java @@ -36,6 +36,7 @@ import mekanism.common.network.to_server.PacketRadialModeChange; import mekanism.common.network.to_server.PacketRemoveModule; import mekanism.common.network.to_server.PacketRobit; +import mekanism.common.network.to_server.PacketSaveMode; import mekanism.common.network.to_server.PacketSecurityMode; import mekanism.common.network.to_server.PacketUpdateModuleSettings; import mekanism.common.network.to_server.PacketWindowSelect; @@ -78,6 +79,7 @@ public void initialize() { registerClientToServer(PacketSecurityMode.class, PacketSecurityMode::decode); registerClientToServer(PacketUpdateModuleSettings.class, PacketUpdateModuleSettings::decode); registerClientToServer(PacketWindowSelect.class, PacketWindowSelect::decode); + registerClientToServer(PacketSaveMode.class, PacketSaveMode::decode); //Server to client messages registerServerToClient(PacketFlyingSync.class, PacketFlyingSync::decode); diff --git a/src/main/java/mekanism/common/network/to_server/PacketSaveMode.java b/src/main/java/mekanism/common/network/to_server/PacketSaveMode.java new file mode 100644 index 00000000000..c26a03ceee9 --- /dev/null +++ b/src/main/java/mekanism/common/network/to_server/PacketSaveMode.java @@ -0,0 +1,40 @@ +package mekanism.common.network.to_server; + +import mekanism.common.item.interfaces.ISaveModeItem; +import mekanism.common.network.IMekanismPacket; +import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.network.NetworkEvent; + +public class PacketSaveMode implements IMekanismPacket { + + private final int slotId; + private final int modeId; + + public PacketSaveMode(int slotId, int modeId) { + this.slotId = slotId; + this.modeId = modeId; + } + + @Override + public void handle(NetworkEvent.Context context) { + Player player = context.getSender(); + if (player != null) { + ItemStack stack = player.getInventory().getItem(slotId); + if (!stack.isEmpty() && stack.getItem() instanceof ISaveModeItem saveModeItem) { + saveModeItem.saveMode(stack, modeId); + } + } + } + + @Override + public void encode(FriendlyByteBuf buffer) { + buffer.writeVarInt(slotId); + buffer.writeVarInt(modeId); + } + + public static PacketSaveMode decode(FriendlyByteBuf buffer) { + return new PacketSaveMode(buffer.readVarInt(), buffer.readVarInt()); + } +} \ No newline at end of file diff --git a/src/main/java/mekanism/common/network/to_server/PacketUpdateModuleSettings.java b/src/main/java/mekanism/common/network/to_server/PacketUpdateModuleSettings.java index 980e029c3cc..640cd85e8b4 100644 --- a/src/main/java/mekanism/common/network/to_server/PacketUpdateModuleSettings.java +++ b/src/main/java/mekanism/common/network/to_server/PacketUpdateModuleSettings.java @@ -54,6 +54,10 @@ public void handle(NetworkEvent.Context context) { List> configItems = module.getConfigItems(); if (dataIndex < configItems.size()) { setValue(configItems.get(dataIndex)); + // This would be the best place to put this if we want it to sync changes to the current mode on the server + // if (stack.getItem() instanceof ISaveModeItem saveModeItem) { + // saveModeItem.saveMode(stack, ItemDataUtils.getInt(stack, NBTConstants.MODE)); + // } } } } diff --git a/src/main/java/mekanism/common/registries/MekanismModules.java b/src/main/java/mekanism/common/registries/MekanismModules.java index 955086b5e73..b9a45218b80 100644 --- a/src/main/java/mekanism/common/registries/MekanismModules.java +++ b/src/main/java/mekanism/common/registries/MekanismModules.java @@ -49,7 +49,7 @@ private MekanismModules() { //Meka-Tool public static final ModuleRegistryObject EXCAVATION_ESCALATION_UNIT = MODULES.register("excavation_escalation_unit", ModuleExcavationEscalationUnit::new, () -> MekanismItems.MODULE_EXCAVATION_ESCALATION.asItem(), builder -> builder.maxStackSize(4).rarity(Rarity.UNCOMMON) - .handlesModeChange().rendersHUD()); + .rendersHUD()); public static final ModuleRegistryObject ATTACK_AMPLIFICATION_UNIT = MODULES.register("attack_amplification_unit", ModuleAttackAmplificationUnit::new, () -> MekanismItems.MODULE_ATTACK_AMPLIFICATION.asItem(), builder -> builder.maxStackSize(4).rarity(Rarity.UNCOMMON) .rendersHUD());