Skip to content

Commit 541f1d2

Browse files
committed
Faster bundle copy, without intermediate bundle
1 parent 0247e54 commit 541f1d2

4 files changed

Lines changed: 48 additions & 29 deletions

File tree

src/main/java/net/evmodder/evmod/Configs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public static class Hotkeys{
229229
public static final ConfigHotkey OPEN_CONFIG_GUI = new ConfigHotkey("openConfigGui", "M,N").apply(HOTKEYS_KEY);
230230

231231
public static final ConfigHotkey MAP_COPY = new ConfigHotkey("mapCopy", "T", KeybindSettings.GUI).apply(HOTKEYS_KEY);
232+
public static final ConfigBoolean MAP_COPY_BUNDLE_BETA = new ConfigBoolean("mapCopyBundleSelectPacket", true).apply(HOTKEYS_KEY);
232233
public static final ConfigHotkey MAP_LOAD = new ConfigHotkey("mapLoad", "E", KeybindSettings.GUI).apply(HOTKEYS_KEY);
233234
public static final ConfigHotkey MAP_MOVE = new ConfigHotkey("mapMove", "T", GUI_ALLOW_EXTRA_KEYS).apply(HOTKEYS_KEY);
234235
public static final ConfigHotkey MAP_MOVE_BUNDLE = new ConfigHotkey("mapMoveBundle", "D", KeybindSettings.GUI).apply(HOTKEYS_KEY);
@@ -347,7 +348,7 @@ public static final List<IConfigBase> getOptions(){
347348
availableOptions = new ArrayList<>();
348349
availableOptions.addAll(List.of(
349350
OPEN_CONFIG_GUI,
350-
MAP_COPY, MAP_LOAD, MAP_MOVE, MAP_MOVE_BUNDLE, MAP_MOVE_BUNDLE_REVERSE,
351+
MAP_COPY, MAP_COPY_BUNDLE_BETA, MAP_LOAD, MAP_MOVE, MAP_MOVE_BUNDLE, MAP_MOVE_BUNDLE_REVERSE,
351352
MAP_CLICK_MOVE_NEIGHBORS, MAP_CLICK_MOVE_NEIGHBORS_KEY
352353
));
353354
if(!Main.mapArtFeaturesOnly) availableOptions.addAll(List.of(

src/main/java/net/evmodder/evmod/keybinds/KeybindMapCopy.java

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package net.evmodder.evmod.keybinds;
22

3+
import net.evmodder.evmod.Configs;
34
import net.evmodder.evmod.Main;
45
import net.evmodder.evmod.apis.ClickUtils.ActionType;
56
import net.evmodder.evmod.apis.ClickUtils.InvAction;
@@ -183,15 +184,16 @@ private void copyMapArtInBundles(final ArrayDeque<InvAction> clicks, final ItemS
183184
final BundleContentsComponent[] bundles = Arrays.stream(slotsWithBundles)
184185
.mapToObj(i -> slots[i].get(DataComponentTypes.BUNDLE_CONTENTS)).toArray(BundleContentsComponent[]::new);
185186
final int SRC_BUNDLES = (int)Arrays.stream(bundles).filter(Predicate.not(BundleContentsComponent::isEmpty)).count();
186-
final int emptyBundles = bundles.length - SRC_BUNDLES;
187-
if(emptyBundles == 1){Main.LOGGER.warn("MapCopyBundle: Could not find an auxiliary bundle"); return;}
187+
final boolean USE_TEMP_BUNDLE = !Configs.Hotkeys.MAP_COPY_BUNDLE_BETA.getBooleanValue();
188+
final int USABLE_EMPTY_BUNDLES = bundles.length - SRC_BUNDLES - (USE_TEMP_BUNDLE ? 1 : 0);
189+
if(USABLE_EMPTY_BUNDLES <= 0){Main.LOGGER.warn("MapCopyBundle: Could not find a usable empty bundle"); return;}
188190
int LAST_EMPTY_SLOT = lastEmptySlot(slots, f.HOTBAR_END, f.INV_START);
189191
if(LAST_EMPTY_SLOT == -1 && Arrays.stream(bundles).anyMatch(b -> b.stream().anyMatch(s -> s.getCount() > 1))){
190192
Main.LOGGER.warn("MapCopyBundle: Unable to copy bundles containing maps with stackSize>1 without an empty inv slot");
191193
return;
192194
}
193-
final int DESTS_PER_SRC = SRC_BUNDLES >= emptyBundles ? 999 : (emptyBundles-1)/SRC_BUNDLES;
194-
Main.LOGGER.warn("MapCopyBundle: source bundles: "+SRC_BUNDLES+", empty bundles: "+emptyBundles
195+
final int DESTS_PER_SRC = SRC_BUNDLES > USABLE_EMPTY_BUNDLES ? 999 : USABLE_EMPTY_BUNDLES/SRC_BUNDLES;
196+
Main.LOGGER.warn("MapCopyBundle: source bundles: "+SRC_BUNDLES+", (potential)destination bundles: "+USABLE_EMPTY_BUNDLES
195197
+", dest-per-src: "+DESTS_PER_SRC+", last-empty-slot: "+LAST_EMPTY_SLOT);
196198

197199
TreeMap<Integer, List<Integer>> bundlesToCopy = new TreeMap<>(); // source bundle -> destination bundles (slotsWithBundles)
@@ -207,7 +209,7 @@ private void copyMapArtInBundles(final ArrayDeque<InvAction> clicks, final ItemS
207209
// Main.LOGGER.info("looking for dest bundles for "+slots[s1].getName().getString()+" in slot "+s1);
208210

209211
if(name1 != null){ // Match by name (1st priority)
210-
for(int j=0; j<slotsWithBundles.length && usedDests.size()+1<emptyBundles && copyDests.size()<DESTS_PER_SRC; ++j){
212+
for(int j=0; j<slotsWithBundles.length && usedDests.size()<USABLE_EMPTY_BUNDLES && copyDests.size()<DESTS_PER_SRC; ++j){
211213
if(bundles[j].isEmpty() && name1.equals(getCustomNameOrNull(slots[slotsWithBundles[j]])) && usedDests.add(j))
212214
{
213215
Main.LOGGER.info("MapCopyBundle: matching-name copy dest "+s1+"->"+slotsWithBundles[j]);
@@ -216,7 +218,7 @@ private void copyMapArtInBundles(final ArrayDeque<InvAction> clicks, final ItemS
216218
}
217219
}
218220
if(copyDests.isEmpty() && slots[s1].getItem() != Items.BUNDLE){ // Match by non-default color (2nd priority)
219-
for(int j=0; j<slotsWithBundles.length && usedDests.size()+1<emptyBundles && copyDests.size()<DESTS_PER_SRC; ++j){
221+
for(int j=0; j<slotsWithBundles.length && usedDests.size()<USABLE_EMPTY_BUNDLES && copyDests.size()<DESTS_PER_SRC; ++j){
220222
if(bundles[j].isEmpty() && slots[s1].getItem() == slots[slotsWithBundles[j]].getItem()
221223
&& (name1 == null || getCustomNameOrNull(slots[slotsWithBundles[j]]) == null) && usedDests.add(j))
222224
{
@@ -226,7 +228,7 @@ private void copyMapArtInBundles(final ArrayDeque<InvAction> clicks, final ItemS
226228
}
227229
}
228230
// If the above methods failed, loosen the src-bundle requirements a bit
229-
if(copyDests.isEmpty()) for(int j=0; j<slotsWithBundles.length && usedDests.size()+1<emptyBundles && copyDests.size()<DESTS_PER_SRC; ++j){
231+
if(copyDests.isEmpty()) for(int j=0; j<slotsWithBundles.length && usedDests.size()<USABLE_EMPTY_BUNDLES && copyDests.size()<DESTS_PER_SRC; ++j){
230232
final int s2 = slotsWithBundles[j];
231233
if(!bundles[j].isEmpty()) continue;
232234
if(name1 != null && getCustomNameOrNull(slots[s2]) != null && !name1.equals(getCustomNameOrNull(slots[s2]))) continue;
@@ -248,37 +250,49 @@ private void copyMapArtInBundles(final ArrayDeque<InvAction> clicks, final ItemS
248250
}
249251
if(bundlesToCopy.isEmpty()){Main.LOGGER.warn("MapCopyBundle: No bundles found to copy"); return;}
250252

251-
HashSet<Integer> unusedBundles = new HashSet<Integer>(slotsWithBundles.length);
252-
for(int i=0; i<slotsWithBundles.length; ++i) unusedBundles.add(i);
253-
for(var e : bundlesToCopy.entrySet()){unusedBundles.remove(e.getKey()); unusedBundles.removeAll(e.getValue());}
254-
assert unusedBundles.size() >= 1;
255-
256-
final int[] unusedBundleSlots = unusedBundles.stream().mapToInt(Integer::intValue).map(i -> slotsWithBundles[i]).toArray();
257-
final boolean anyUnnamedDst = bundlesToCopy.values().stream().anyMatch(d -> d.stream().anyMatch(i -> slots[slotsWithBundles[i]].getCustomName() == null));
258-
final PrioAndSlot pas = Arrays.stream(unusedBundleSlots).mapToObj(i ->
259-
// Lower score is better
260-
new PrioAndSlot(
261-
(!anyUnnamedDst && slots[i].getCustomName() != null ? 4 : 0)
262-
+ (srcBundleTypes.contains(slots[i].getItem()) ? 2 : 0)
263-
+ (dstBundleTypes.contains(slots[i].getItem()) ? 1 : 0), i)).min(Comparator.naturalOrder()).get();
264-
Main.LOGGER.info("MapCopyBundle: Intermediary bundle: slot="+pas.slot+", uniquelyUnnamed="+((pas.p&4)==0)
265-
+", uniqueFromSrcType="+((pas.p&2)==0)+", uniqueFromDstType="+((pas.p&1)==0));
266-
267-
final int tempBundleSlot = pas.slot;
253+
final int tempBundleSlot;
254+
if(!USE_TEMP_BUNDLE) tempBundleSlot = -1;
255+
else{
256+
HashSet<Integer> unusedBundles = new HashSet<Integer>(slotsWithBundles.length);
257+
for(int i=0; i<slotsWithBundles.length; ++i) unusedBundles.add(i);
258+
for(var e : bundlesToCopy.entrySet()){unusedBundles.remove(e.getKey()); unusedBundles.removeAll(e.getValue());}
259+
assert unusedBundles.size() >= 1;
260+
final int[] unusedBundleSlots = unusedBundles.stream().mapToInt(Integer::intValue).map(i -> slotsWithBundles[i]).toArray();
261+
final boolean anyUnnamedDst = bundlesToCopy.values().stream().anyMatch(d -> d.stream().anyMatch(i -> slots[slotsWithBundles[i]].getCustomName() == null));
262+
final PrioAndSlot pas = Arrays.stream(unusedBundleSlots).mapToObj(i ->
263+
// Lower score is better
264+
new PrioAndSlot(
265+
(!anyUnnamedDst && slots[i].getCustomName() != null ? 4 : 0)
266+
+ (srcBundleTypes.contains(slots[i].getItem()) ? 2 : 0)
267+
+ (dstBundleTypes.contains(slots[i].getItem()) ? 1 : 0), i)).min(Comparator.naturalOrder()).get();
268+
tempBundleSlot = pas.slot;
269+
Main.LOGGER.info("MapCopyBundle: Intermediary bundle: slot="+pas.slot+", uniquelyUnnamed="+((pas.p&4)==0)
270+
+", uniqueFromSrcType="+((pas.p&2)==0)+", uniqueFromDstType="+((pas.p&1)==0));
271+
}
272+
273+
final boolean BUNDLE_SELECT_REVERSE = !USE_TEMP_BUNDLE && Configs.Generic.BUNDLE_SELECT_REVERSED.getBooleanValue();
268274
for(var entry : bundlesToCopy.entrySet()){
275+
BundleContentsComponent content = bundles[entry.getKey()];
269276
// Main.LOGGER.info("MapCopyBundle: Copying map bundle in slot "+k+", "+slots[k].getName().getString()+" to slots: "+bundlesToCopy.get(k));
270-
for(int _0=0; _0<bundles[entry.getKey()].size(); ++_0){
277+
if(USE_TEMP_BUNDLE) for(int _0=0; _0<content.size(); ++_0){
271278
clicks.add(new InvAction(slotsWithBundles[entry.getKey()], 1, ActionType.CLICK)); // Take last map from src bundle
272279
clicks.add(new InvAction(tempBundleSlot, 0, ActionType.CLICK)); // Place map in temp bundle
273280
}
274281
// Main.LOGGER.info("MapCopyBundle: Move to intermediary bundle complete, beginning copy");
275282

276283
//2+4+2 vs 2+3+2
277284
//bundles[k].stream().mapToInt(stack -> stack.getCount()).forEach(count -> {
278-
for(int i=0; i<bundles[entry.getKey()].size(); ++i){
279-
final int count = bundles[entry.getKey()].get(i).getCount();
285+
for(int i=0; i<content.size(); ++i){
286+
final int count = content.get(i).getCount();
280287

281-
clicks.add(new InvAction(tempBundleSlot, 1, ActionType.CLICK)); // Take last map from temp bundle
288+
if(USE_TEMP_BUNDLE) clicks.add(new InvAction(tempBundleSlot, 1, ActionType.CLICK)); // Take map from temp bundle
289+
else{
290+
final int innerBundleSlot = BUNDLE_SELECT_REVERSE ? content.size()-1 : 0;
291+
// final int innerBundleSlot = BUNDLE_SELECT_REVERSE ? content.size()-(i+1) : i;
292+
// final int innerBundleSlot = BUNDLE_SELECT_REVERSE ? i : content.size()-(i+1);
293+
clicks.add(new InvAction(slotsWithBundles[entry.getKey()], innerBundleSlot, ActionType.BUNDLE_SELECT)); // Select 1st map in bundle
294+
clicks.add(new InvAction(slotsWithBundles[entry.getKey()], 1, ActionType.CLICK)); // Take map from src bundle
295+
}
282296
clicks.add(new InvAction(f.INPUT_START, 0, ActionType.CLICK)); // Place in crafter
283297
boolean didShiftCraft = false;
284298
//Main.LOGGER.info("MapCopyBundle: Coping map item into "+bundlesToCopy.get(k).size()+" dest bundles");

src/main/resources/assets/evmod/lang/en_us.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@
152152
"evmod.config.hotkeys.name.openConfigGui": "openConfigGui",
153153
"evmod.config.hotkeys.name.mapCopy": "mapCopy",
154154
"evmod.config.hotkeys.comment.mapCopy": "Copy maps in inv or in bundles",
155+
"evmod.config.hotkeys.name.mapCopyBundleSelectPacket": "mapCopyBundleSelectPacket",
156+
"evmod.config.hotkeys.comment.mapCopyBundleSelectPacket": "Enable a BETA speed enhancement, for bundle-copy",
155157
"evmod.config.hotkeys.name.mapLoad": "mapLoad",
156158
"evmod.config.hotkeys.comment.mapLoad": "Load map previews for container/bundle",
157159
"evmod.config.hotkeys.name.mapMove": "mapMove",

src/main/resources/assets/evmod/lang/es_ar.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@
147147
"evmod.config.hotkeys.name.openConfigGui": "openConfigGui",
148148
"evmod.config.hotkeys.name.mapCopy": "mapCopy",
149149
"evmod.config.hotkeys.comment.mapCopy": "Copy maps in inv or in bundles",
150+
"evmod.config.hotkeys.name.mapCopyBundleSelectPacket": "mapCopyBundleSelectPacket",
151+
"evmod.config.hotkeys.comment.mapCopyBundleSelectPacket": "Enable a BETA speed enhancement, for bundle-copy",
150152
"evmod.config.hotkeys.name.mapLoad": "mapLoad",
151153
"evmod.config.hotkeys.comment.mapLoad": "Load map previews for container/bundle",
152154
"evmod.config.hotkeys.name.mapMove": "mapMove",

0 commit comments

Comments
 (0)