|
1 | 1 | package net.evmodder.evmod.keybinds; |
2 | 2 |
|
3 | 3 | import java.util.ArrayDeque; |
4 | | -import java.util.Arrays; |
5 | 4 | import java.util.Comparator; |
6 | 5 | import java.util.HashSet; |
7 | 6 | import java.util.IdentityHashMap; |
8 | 7 | import java.util.Optional; |
9 | 8 | import java.util.OptionalInt; |
10 | 9 | import java.util.stream.IntStream; |
| 10 | +import net.evmodder.evmod.Configs; |
11 | 11 | import net.evmodder.evmod.Main; |
12 | 12 | import net.evmodder.evmod.apis.ClickUtils.ActionType; |
13 | 13 | import net.evmodder.evmod.apis.ClickUtils.InvAction; |
@@ -59,53 +59,74 @@ private int[] getUsableHotbarButtons(MinecraftClient client){ |
59 | 59 | return IntStream.range(0, 9).filter(hb -> isUsable(client.player.getInventory().getStack(hb))).toArray(); |
60 | 60 | } |
61 | 61 |
|
62 | | - private final long WAIT_FOR_STATE_UPDATE = 71, STATE_LOAD_TIMEOUT = 5*1000; // 50 = 1 tick |
| 62 | + private final long WAIT_FOR_STATE_UPDATE = 101, STATE_LOAD_TIMEOUT = 5*1000; // 50 = 1 tick |
63 | 63 | private long stateUpdateWaitStart, stateLoadWaitStart; |
64 | 64 | private final void loadMapArtFromBundles(){ |
65 | 65 | // Main.LOGGER.warn("MapLoadBundle: in InventoryScreen"); |
66 | 66 | final MinecraftClient client = MinecraftClient.getInstance(); |
67 | 67 | final InventoryScreen is = (InventoryScreen)client.currentScreen; |
68 | 68 | final ItemStack[] slots = is.getScreenHandler().slots.stream().map(s -> s.getStack()).toArray(ItemStack[]::new); |
69 | | - final int[] slotsWithBundles = IntStream.range(9, 45).filter(i -> slots[i].get(DataComponentTypes.BUNDLE_CONTENTS) != null).toArray(); |
70 | | - if(slotsWithBundles.length == 0){ |
71 | | - Main.LOGGER.warn("MapLoadBundle: No bundles in inventory"); |
| 69 | + final int[] slotsWithMapArtBundles = IntStream.range(9, 45).filter(i -> { |
| 70 | + BundleContentsComponent content = slots[i].get(DataComponentTypes.BUNDLE_CONTENTS); |
| 71 | + return content != null && !content.isEmpty() && content.stream().allMatch(s -> s.getItem() == Items.FILLED_MAP); |
| 72 | + }).toArray(); |
| 73 | + if(slotsWithMapArtBundles.length == 0){ |
| 74 | + Main.LOGGER.warn("MapLoadBundle: No mapart bundles in inventory"); |
72 | 75 | return; |
73 | 76 | } |
74 | | - final OptionalInt emptyBundleSlotOpt = Arrays.stream(slotsWithBundles) |
75 | | - .filter(i -> slots[i].get(DataComponentTypes.BUNDLE_CONTENTS).getOccupancy().getNumerator() == 0).findAny(); |
76 | | - if(emptyBundleSlotOpt.isEmpty()){Main.LOGGER.warn("MapLoadBundle: Empty bundle not found"); return;} |
77 | | - final int emptyBundleSlot = emptyBundleSlotOpt.getAsInt(); |
| 77 | + final boolean USE_TEMP_BUNDLE = !Configs.Generic.BUNDLE_SELECT_PACKET.getBooleanValue(); |
| 78 | + int emptyBundleSlot = -1; |
| 79 | + if(USE_TEMP_BUNDLE){ |
| 80 | + final OptionalInt emptyBundleSlotOpt = IntStream.range(9, 45).filter(i -> slots[i].get(DataComponentTypes.BUNDLE_CONTENTS).isEmpty()).findAny(); |
| 81 | + if(emptyBundleSlotOpt.isEmpty()){Main.LOGGER.warn("MapLoadBundle: Empty bundle not found"); return;} |
| 82 | + emptyBundleSlot = emptyBundleSlotOpt.getAsInt(); |
| 83 | + } |
78 | 84 | // final OptionalInt emptySlotOpt = IntStream.range(9, 45).filter(i -> slots[i].isEmpty()).min(Comparator.comparingInt(i -> Math.abs(i-emptyBundleSlot))); |
79 | 85 | Optional<Integer> emptySlotOpt = IntStream.range(9, 45).filter(i -> slots[i].isEmpty()).boxed() |
80 | | - .min(Comparator.comparingInt(i -> Math.abs(i-emptyBundleSlot))); |
| 86 | + .min(Comparator.comparingInt(i -> Math.abs(i-slotsWithMapArtBundles[0]))); |
81 | 87 | if(emptySlotOpt.isEmpty()){Main.LOGGER.warn("MapLoadBundle: Empty slot not found"); return;} |
82 | 88 | final int emptySlot = emptySlotOpt.get(); |
83 | 89 |
|
84 | 90 | final ArrayDeque<InvAction> clicks = new ArrayDeque<>(); |
85 | 91 | final IdentityHashMap<InvAction, Integer> ableToSkipClicks = new IdentityHashMap<>(); |
86 | | - for(int i : slotsWithBundles){ |
| 92 | + for(int i : slotsWithMapArtBundles){ |
87 | 93 | BundleContentsComponent contents = slots[i].get(DataComponentTypes.BUNDLE_CONTENTS); |
88 | 94 | if(contents.isEmpty()) continue; |
89 | | - if(contents.stream().anyMatch(s -> s.getItem() != Items.FILLED_MAP)) continue; // Skip bundles with non-mapart contents |
| 95 | +// if(contents.stream().anyMatch(s -> s.getItem() != Items.FILLED_MAP)) continue; // Skip bundles with non-mapart contents |
90 | 96 | if(contents.stream().allMatch(s -> isLoadedMapArt(client.world, s))) continue; // Skip bundles with already-loaded mapart |
91 | 97 | // Main.LOGGER.info("MapLoadBundle: found bundle with "+contents.size()+" maps"); |
92 | | - for(int j=0; j<contents.size(); ++j){ |
93 | | - InvAction c; |
94 | | - clicks.add(c=new InvAction(i, 1, ActionType.CLICK)); // Take last map from bundle |
95 | | - clicks.add(new InvAction(emptySlot, 0, ActionType.CLICK)); // Place map in empty slot |
96 | | - // Wait for map state to load |
97 | | - clicks.add(new InvAction(emptySlot, 0, ActionType.CLICK)); // Take map from empty slot |
98 | | - clicks.add(new InvAction(emptyBundleSlot, 0, ActionType.CLICK)); // Place map in empty bundle |
| 98 | + if(USE_TEMP_BUNDLE){ |
| 99 | + for(int j=0; j<contents.size(); ++j){ |
| 100 | + InvAction c; |
| 101 | + clicks.add(c=new InvAction(i, 1, ActionType.CLICK)); // Take last map from bundle |
| 102 | + clicks.add(new InvAction(emptySlot, 0, ActionType.CLICK)); // Place map in empty slot |
| 103 | + // Wait for map state to load |
| 104 | + clicks.add(new InvAction(emptySlot, 0, ActionType.CLICK)); // Take map from empty slot |
| 105 | + clicks.add(new InvAction(emptyBundleSlot, 0, ActionType.CLICK)); // Place map in empty bundle |
99 | 106 |
|
100 | | - ableToSkipClicks.put(c, 6*(contents.size()-j)); |
| 107 | + ableToSkipClicks.put(c, 6*(contents.size()-j)); |
| 108 | + } |
| 109 | + for(int j=0; j<contents.size(); ++j){ |
| 110 | + clicks.add(new InvAction(emptyBundleSlot, 1, ActionType.CLICK)); // Take last map from empty bundle |
| 111 | + clicks.add(new InvAction(i, 0, ActionType.CLICK)); // Place map back in original bundle |
| 112 | + } |
101 | 113 | } |
102 | | - for(int j=0; j<contents.size(); ++j){ |
103 | | - clicks.add(new InvAction(emptyBundleSlot, 1, ActionType.CLICK)); // Take last map from empty bundle |
104 | | - clicks.add(new InvAction(i, 0, ActionType.CLICK)); // Place map back in original bundle |
| 114 | + else{ |
| 115 | + for(int j=0; j<contents.size(); ++j){ |
| 116 | + if(contents.size() > 1){ |
| 117 | + int bottomBundleSlot = Configs.Generic.BUNDLE_SELECT_REVERSED.getBooleanValue() ? contents.size()-1 : 0; |
| 118 | + clicks.add(new InvAction(i, bottomBundleSlot, ActionType.BUNDLE_SELECT)); // Select 1st map in bundle |
| 119 | + } |
| 120 | + clicks.add(new InvAction(i, 1, ActionType.CLICK)); // Take 1st map from bundle |
| 121 | + clicks.add(new InvAction(emptySlot, 0, ActionType.CLICK)); // Place map in empty slot |
| 122 | + // Wait for map state to load |
| 123 | + clicks.add(new InvAction(emptySlot, 0, ActionType.CLICK)); // Take map from empty slot |
| 124 | + clicks.add(new InvAction(i, 0, ActionType.CLICK)); // Place map back into bundle |
| 125 | + } |
105 | 126 | } |
106 | 127 | } |
107 | 128 | if(clicks.isEmpty()){ |
108 | | - Main.LOGGER.warn("MapLoadBundle: No bundles containing mapart in inventory"); |
| 129 | + Main.LOGGER.warn("MapLoadBundle: No bundles containing unloaded mapart in inventory"); |
109 | 130 | return; // No bundles with maps |
110 | 131 | } |
111 | 132 | // client.player.sendMessage(Text.literal("Scheduling clicks: "+clicks.size()), true); |
@@ -134,7 +155,7 @@ private final void loadMapArtFromBundles(){ |
134 | 155 | Main.LOGGER.warn("MapLoadBundle: Timed out while waiting for map state to load!"); |
135 | 156 | return true; |
136 | 157 | } |
137 | | - // Wait a bit even aft map state is loaded, to ensure it REALLY gets loaded |
| 158 | + // Wait a bit even after map state is loaded, to ensure it REALLY gets loaded |
138 | 159 | else if(stateUpdateWaitStart <= 0){stateUpdateWaitStart = System.currentTimeMillis(); return false;} |
139 | 160 | else if(System.currentTimeMillis() - stateUpdateWaitStart < WAIT_FOR_STATE_UPDATE) return false; |
140 | 161 | else{stateUpdateWaitStart = 0; return true;} |
|
0 commit comments