Combine adjacent screen handler inv ranges. Fixes #2365
This commit is contained in:
parent
3fa83d8f63
commit
f24e8f0631
2 changed files with 62 additions and 2 deletions
|
@ -43,6 +43,7 @@ import reborncore.common.blockentity.MachineBaseBlockEntity;
|
||||||
import reborncore.common.network.ClientBoundPackets;
|
import reborncore.common.network.ClientBoundPackets;
|
||||||
import reborncore.common.network.NetworkManager;
|
import reborncore.common.network.NetworkManager;
|
||||||
import reborncore.common.util.ItemUtils;
|
import reborncore.common.util.ItemUtils;
|
||||||
|
import reborncore.common.util.RangeUtil;
|
||||||
import reborncore.mixin.ifaces.ServerPlayerEntityScreenHandler;
|
import reborncore.mixin.ifaces.ServerPlayerEntityScreenHandler;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -78,8 +79,8 @@ public class BuiltScreenHandler extends ScreenHandler {
|
||||||
|
|
||||||
this.canInteract = canInteract;
|
this.canInteract = canInteract;
|
||||||
|
|
||||||
this.playerSlotRanges = playerSlotRange;
|
this.playerSlotRanges = RangeUtil.joinAdjacent(playerSlotRange);
|
||||||
this.blockEntitySlotRanges = blockEntitySlotRange;
|
this.blockEntitySlotRanges = RangeUtil.joinAdjacent(blockEntitySlotRange);
|
||||||
|
|
||||||
this.blockEntity = blockEntity;
|
this.blockEntity = blockEntity;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
/*
|
||||||
|
* This file is part of RebornCore, licensed under the MIT License (MIT).
|
||||||
|
*
|
||||||
|
* Copyright (c) 2021 TeamReborn
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package reborncore.common.util;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.Range;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public final class RangeUtil {
|
||||||
|
private RangeUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Range<Integer>> joinAdjacent(List<Range<Integer>> list) {
|
||||||
|
var in = list.stream().sorted(Comparator.comparing(Range::getMinimum)).toList();
|
||||||
|
var out = new LinkedList<Range<Integer>>();
|
||||||
|
|
||||||
|
for (var range : in) {
|
||||||
|
if (out.isEmpty()) {
|
||||||
|
out.add(range);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var last = out.getLast();
|
||||||
|
if (last.getMaximum() >= range.getMinimum() -1) {
|
||||||
|
out.removeLast();
|
||||||
|
out.add(Range.between(last.getMinimum(), range.getMaximum()));
|
||||||
|
} else {
|
||||||
|
out.add(range);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Collections.unmodifiableList(out);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue