|
| 1 | +package io.github.samipourquoi.moretoolbars.mixin; |
| 2 | + |
| 3 | +import io.github.samipourquoi.moretoolbars.Keybinds; |
| 4 | +import net.fabricmc.api.EnvType; |
| 5 | +import net.fabricmc.api.Environment; |
| 6 | +import net.minecraft.client.MinecraftClient; |
| 7 | +import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen; |
| 8 | +import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen; |
| 9 | +import net.minecraft.client.network.ClientPlayerEntity; |
| 10 | +import net.minecraft.client.option.HotbarStorage; |
| 11 | +import net.minecraft.client.option.HotbarStorageEntry; |
| 12 | +import net.minecraft.client.option.KeyBinding; |
| 13 | +import net.minecraft.entity.player.PlayerInventory; |
| 14 | +import net.minecraft.item.Item; |
| 15 | +import net.minecraft.item.ItemGroup; |
| 16 | +import net.minecraft.item.ItemStack; |
| 17 | +import net.minecraft.item.Items; |
| 18 | +import net.minecraft.text.OrderedText; |
| 19 | +import net.minecraft.text.Text; |
| 20 | +import net.minecraft.text.TranslatableText; |
| 21 | +import org.spongepowered.asm.mixin.Mixin; |
| 22 | +import org.spongepowered.asm.mixin.Overwrite; |
| 23 | +import org.spongepowered.asm.mixin.injection.*; |
| 24 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 25 | + |
| 26 | +@Environment(EnvType.CLIENT) |
| 27 | +@Mixin(CreativeInventoryScreen.class) |
| 28 | +public abstract class MixinCreativeInventoryScreen extends AbstractInventoryScreen<CreativeInventoryScreen.CreativeScreenHandler> { |
| 29 | + |
| 30 | + public MixinCreativeInventoryScreen(CreativeInventoryScreen.CreativeScreenHandler container, PlayerInventory playerInventory, Text text) { |
| 31 | + super(container, playerInventory, text); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @author samipourquoi |
| 36 | + */ |
| 37 | + @Overwrite |
| 38 | + public static void onHotbarKeyPress(MinecraftClient client, int index, boolean restore, boolean save) { |
| 39 | + ClientPlayerEntity clientPlayerEntity = client.player; |
| 40 | + HotbarStorage hotbarStorage = client.getCreativeHotbarStorage(); |
| 41 | + HotbarStorageEntry hotbarStorageEntry = hotbarStorage.getSavedHotbar(index); |
| 42 | + int j; |
| 43 | + if (clientPlayerEntity == null) return; |
| 44 | + if (restore) { |
| 45 | + for(j = 0; j < PlayerInventory.getHotbarSize(); j++) { |
| 46 | + ItemStack itemStack = ((ItemStack)hotbarStorageEntry.get(j)).copy(); |
| 47 | + clientPlayerEntity.inventory.setStack(j, itemStack); |
| 48 | + client.interactionManager.clickCreativeStack(itemStack, 36 + j); |
| 49 | + } |
| 50 | + |
| 51 | + clientPlayerEntity.playerScreenHandler.sendContentUpdates(); |
| 52 | + } else if (save) { |
| 53 | + for(j = 0; j < PlayerInventory.getHotbarSize(); j++) { |
| 54 | + hotbarStorageEntry.set(j, clientPlayerEntity.inventory.getStack(j).copy()); |
| 55 | + } |
| 56 | + |
| 57 | + String hotbarKey = getTranslationName(client.options.keysHotbar[index % 9]); |
| 58 | + String restoreKey = getTranslationName(client.options.keyLoadToolbarActivator); |
| 59 | + String[] modifiers = { |
| 60 | + (Keybinds.group1.isUnbound()) ? "" : getTranslationName(Keybinds.group1) + "+", |
| 61 | + (Keybinds.group2.isUnbound()) ? "" : getTranslationName(Keybinds.group2) + "+", |
| 62 | + (Keybinds.group3.isUnbound()) ? "" : getTranslationName(Keybinds.group3) + "+", |
| 63 | + }; |
| 64 | + |
| 65 | + TranslatableText originalText = new TranslatableText("inventory.hotbarSaved", modifiers[index/9] + restoreKey, hotbarKey); |
| 66 | + |
| 67 | + client.inGameHud.setOverlayMessage((Text) originalText, false); |
| 68 | + hotbarStorage.save(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private static String getTranslationName(KeyBinding key) { |
| 73 | + String a = new TranslatableText(key.getBoundKeyTranslationKey()).asString(); |
| 74 | + String b = key.getBoundKeyLocalizedText().getString(); |
| 75 | + return (b.equals("")) ? a : b; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Cancels the render of the saved toolbars. |
| 80 | + */ |
| 81 | + @ModifyConstant( |
| 82 | + method = "setSelectedTab", |
| 83 | + slice = @Slice( |
| 84 | + from = @At( |
| 85 | + value = "HEAD" |
| 86 | + ), |
| 87 | + to = @At( |
| 88 | + value = "INVOKE", |
| 89 | + target = "Lnet/minecraft/client/options/HotbarStorage;getSavedHotbar(I)Lnet/minecraft/client/options/HotbarStorageEntry;" |
| 90 | + ) |
| 91 | + ), |
| 92 | + constant = @Constant(intValue = 9) |
| 93 | + ) |
| 94 | + private int noOldRenderSavedToolbars(int number) { |
| 95 | + return 0; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * "Overwrites" the render of the saved toolbars. |
| 100 | + */ |
| 101 | + @Inject( |
| 102 | + method = "setSelectedTab", |
| 103 | + at = @At( |
| 104 | + value = "INVOKE", |
| 105 | + target = "Lnet/minecraft/client/MinecraftClient;getCreativeHotbarStorage()Lnet/minecraft/client/options/HotbarStorage;" |
| 106 | + ) |
| 107 | + ) |
| 108 | + private void renderSavedToolbars(ItemGroup group, CallbackInfo info) { |
| 109 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 110 | + HotbarStorage hotbarStorage = client.getCreativeHotbarStorage(); |
| 111 | + Item[] displayItems = {Items.PAPER, Items.MAP, Items.FILLED_MAP}; |
| 112 | + int displayItemIndex = 0; |
| 113 | + String[] modifiers = { |
| 114 | + (Keybinds.group1.isUnbound()) ? "" : getTranslationName(Keybinds.group1) + "+", |
| 115 | + (Keybinds.group2.isUnbound()) ? "" : getTranslationName(Keybinds.group2) + "+", |
| 116 | + (Keybinds.group3.isUnbound()) ? "" : getTranslationName(Keybinds.group3) + "+", |
| 117 | + }; |
| 118 | + |
| 119 | + for(int i = 0; i < 27; i++) { |
| 120 | + HotbarStorageEntry hotbarStorageEntry = hotbarStorage.getSavedHotbar(i); |
| 121 | + String hotbarKey; |
| 122 | + String saveKey; |
| 123 | + |
| 124 | + if ((i % 9 == 0) && i != 0) displayItemIndex++; |
| 125 | + if (hotbarStorageEntry.isEmpty()) { |
| 126 | + |
| 127 | + for(int j = 0; j < 9; j++) { |
| 128 | + if (i % 9 == j) { |
| 129 | + ItemStack itemStack = new ItemStack(displayItems[displayItemIndex]); |
| 130 | + itemStack.getOrCreateSubTag("CustomCreativeLock"); |
| 131 | + |
| 132 | + hotbarKey = getTranslationName(client.options.keysHotbar[j]); |
| 133 | + saveKey = getTranslationName(client.options.keySaveToolbarActivator); |
| 134 | + itemStack.setCustomName(new TranslatableText( |
| 135 | + "inventory.hotbarInfo", |
| 136 | + modifiers[displayItemIndex]+saveKey, |
| 137 | + hotbarKey |
| 138 | + )); |
| 139 | + this.getScreenHandler().itemList.add(itemStack); |
| 140 | + } else { |
| 141 | + this.getScreenHandler().itemList.add(ItemStack.EMPTY); |
| 142 | + } |
| 143 | + } |
| 144 | + } else { |
| 145 | + this.getScreenHandler().itemList.addAll(hotbarStorageEntry); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments