Skip to content

Commit

Permalink
Fix sorting of materials
Browse files Browse the repository at this point in the history
  • Loading branch information
jyhsu2000 committed Dec 1, 2018
1 parent 034e68d commit 22bc7e0
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
1 change: 0 additions & 1 deletion BannerMaker.iml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.bstats:bstats-bukkit:1.4" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.bukkit:bukkit:1.13.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: commons-lang:commons-lang:2.6" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Logs
## Unreleased Version
- Nothing
- Fix sorting of materials

## v2.0.0 (for 1.13.x)
- Update to 1.13.2
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.zh-tw.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 更新紀錄
## 未釋出版本
- 暫無
- 修正材料的排序

## v2.0.0 (for 1.13.x)
- 升級至1.13.2
Expand Down
11 changes: 2 additions & 9 deletions src/main/java/club/kid7/bannermaker/util/BannerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,8 @@ static public List<ItemStack> getMaterials(ItemStack banner) {
//加到暫存清單
List<ItemStack> patternMaterials = new ArrayList<>();
Collections.addAll(patternMaterials, materialInventory.getContents());
//移除空值
patternMaterials.removeAll(Collections.singletonList(null));
//FIXME: 重新排序
// Collections.sort(patternMaterials, (itemStack1, itemStack2) -> {
// if (itemStack1.getTypeId() != itemStack2.getTypeId()) {
// return itemStack1.getTypeId() - itemStack2.getTypeId();
// }
// return itemStack1.getDurability() - itemStack2.getDurability();
// });
//重新排序
InventoryUtil.sort(patternMaterials);
//將材料加到清單中
materialList.addAll(patternMaterials);

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/club/kid7/bannermaker/util/InventoryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class InventoryUtil {
/**
Expand All @@ -20,4 +23,35 @@ public static void give(Player player, ItemStack itemStack) {
player.getWorld().dropItem(player.getLocation(), itemsCanNotAddToInv.get(0));
}
}

public static void sort(List<ItemStack> itemStacks) {
//移除空值
itemStacks.removeAll(Collections.singletonList(null));
//重新排序
itemStacks.sort((itemStack1, itemStack2) -> {
int c = Integer.compare(itemStack1.getType().ordinal(), itemStack2.getType().ordinal());
if (c == 0) {
c = -Integer.compare(itemStack1.getAmount(), itemStack2.getAmount());
}
return c;
});
//合併
ItemStack previous = null;
final Iterator<ItemStack> iterator = itemStacks.iterator();
while (iterator.hasNext()) {
final ItemStack item = iterator.next();
if (previous != null && previous.isSimilar(item) && previous.getAmount() < previous.getMaxStackSize()) {
int count = Math.min(item.getAmount(), previous.getMaxStackSize() - previous.getAmount());
if (count > 0) {
previous.setAmount(previous.getAmount() + count);
item.setAmount(item.getAmount() - count);
if (item.getAmount() <= 0) {
iterator.remove();
continue;
}
}
}
previous = item;
}
}
}

0 comments on commit 22bc7e0

Please sign in to comment.