Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
[ci skip] click blank for paging
Browse files Browse the repository at this point in the history
  • Loading branch information
Librazy committed Mar 23, 2019
1 parent 448d12a commit 765b583
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 76 deletions.
113 changes: 41 additions & 72 deletions src/main/java/cat/nyaa/nyaautils/extrabackpack/ExtraBackpackGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
import cat.nyaa.nyaacore.utils.ItemStackUtils;
import cat.nyaa.nyaautils.I18n;
import cat.nyaa.nyaautils.NyaaUtils;
import com.google.common.collect.MultimapBuilder;
import com.google.common.collect.SetMultimap;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.permissions.Permission;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
Expand All @@ -26,8 +25,6 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.bukkit.event.inventory.InventoryAction.COLLECT_TO_CURSOR;

public class ExtraBackpackGUI implements InventoryHolder {

private final NyaaUtils plugin;
Expand Down Expand Up @@ -98,34 +95,17 @@ void open(int page) {
lastState.put(owner, lines.stream().map(ExtraBackpackLine::getItems).collect(Collectors.toList()));
query.commit();
}
int pageCount = (int) Math.ceil(maxLine / 5.0);
List<ExtraBackpackLine> view = lines.stream().skip(page * 5).limit(5).collect(Collectors.toList());
int pageCount = (int) Math.ceil(maxLine / 6.0);
List<ExtraBackpackLine> view = lines.stream().skip(page * 6).limit(6).collect(Collectors.toList());
int viewSize = view.size();
if (viewSize == 0) {
new Message(I18n.format("user.backpack.invalid_page", page, pageCount)).send(opener);
return;
}
int size = viewSize * 9;
if (lines.size() > 5) {
size += 9;
}
Inventory inventory = Bukkit.createInventory(this, size, I18n.format("user.backpack.title", Bukkit.getOfflinePlayer(owner).getName(), page, pageCount));
ItemStack[] itemStacks = view.stream().flatMap(l -> l.getItemStacks().stream()).toArray(ItemStack[]::new);
inventory.setContents(itemStacks);
if (page > 0) {
ItemStack back = new ItemStack(Material.ARROW);
ItemMeta backItemMeta = back.getItemMeta();
Objects.requireNonNull(backItemMeta).setDisplayName(I18n.format("user.backpack.back"));
back.setItemMeta(backItemMeta);
inventory.setItem(viewSize * 9, back);
}
if (page + 1 < pageCount) {
ItemStack nextPage = new ItemStack(Material.ARROW);
ItemMeta nextPageMeta = nextPage.getItemMeta();
Objects.requireNonNull(nextPageMeta).setDisplayName(I18n.format("user.backpack.next"));
nextPage.setItemMeta(nextPageMeta);
inventory.setItem(viewSize * 9 + 8, nextPage);
}
opener.openInventory(inventory);

daemonTask = new BukkitRunnable() {
Expand All @@ -147,46 +127,41 @@ void onInventoryClick(InventoryClickEvent event) {
event.setCancelled(true);
return;
}
int slot = event.getRawSlot();
Inventory inventory = event.getInventory();
if (maxLine > 5) {
int bottomLine = 5;
if (maxLine - 5 * (currentPage + 1) < 0) {
bottomLine = maxLine - currentPage * 5;
}
if (event.getRawSlot() == bottomLine * 9 && event.getCurrentItem() != null && event.getCurrentItem().getType() != Material.AIR) {
if (event.getAction() == InventoryAction.NOTHING && event.getSlotType() == InventoryType.SlotType.OUTSIDE) {
if (event.isLeftClick()) {
if (maxLine <= 6) return;
event.setCancelled(true);
saveAll(inventory);
close();
this.open(currentPage - 1);
this.open(prevPage());
return;
} else if (event.getRawSlot() == bottomLine * 9 + 8 && event.getCurrentItem() != null && event.getCurrentItem().getType() != Material.AIR) {
} else if (event.isRightClick()) {
if (maxLine <= 6) return;
event.setCancelled(true);
saveAll(inventory);
close();
this.open(currentPage + 1);
return;
} else if (event.getRawSlot() >= bottomLine * 9) {
event.setCancelled(true);
this.open(nextPage());
return;
}
}
Bukkit.getScheduler().runTask(plugin, () -> saveAll(inventory));
}

Bukkit.getScheduler().runTask(plugin, () -> {
if (event.getAction() == COLLECT_TO_CURSOR) {
saveAll(inventory);
} else {
int invLineIndex = slot / 9;
List<ItemStack> line = Arrays.stream(inventory.getContents()).skip(invLineIndex * 9).limit(9).collect(Collectors.toList());
if (line.size() != 9) {
throw new IllegalStateException("Invalid line");
}
if (!saveLine(invLineIndex, line)) {
close();
new Message(I18n.format("user.backpack.error_saving")).send(opener);
}
}
});
private int prevPage() {
int prev = currentPage - 1;
if (prev < 0) {
prev = (int) (Math.ceil(maxLine / 6.0) - 1);
}
return prev;
}

private int nextPage() {
int next = currentPage + 1;
if (next >= Math.ceil(maxLine / 6.0)) {
next = 0;
}
return next;
}

void onInventoryClose(InventoryCloseEvent event) {
Expand All @@ -200,52 +175,46 @@ void onInventoryClose(InventoryCloseEvent event) {
}

private void saveAll(Inventory inventory) {
for (int i = 0; i < inventory.getSize() / 9 - (maxLine > 5 ? 1 : 0); ++i) {
boolean saved = true;
for (int i = 0; i < inventory.getSize() / 9; ++i) {
List<ItemStack> line = Arrays.stream(inventory.getContents()).skip(i * 9).limit(9).collect(Collectors.toList());
saveLine(i, line);
saved &= saveLine(i, line);
}
if (!saved) {
close();
new Message(I18n.format("user.backpack.error_saving")).send(opener);
}
}

private boolean saveLine(int i, List<ItemStack> line) {
int lineNo = currentPage * 5 + i;
String lineLastState = lastState.get(owner).get(lineNo);
String desiredState = ItemStackUtils.itemsToBase64(line);
if (lineLastState.equals(desiredState)) return true;
try (Query<ExtraBackpackLine> query = database.queryTransactional(ExtraBackpackLine.class).whereEq("player_id", owner.toString()).whereEq("line_no", lineNo)) {
ExtraBackpackLine backpackLine = query.selectUniqueForUpdate();
String lineLastState = lastState.get(owner).get(lineNo);
String lineCurrentState = backpackLine.getItems();
if (!lineLastState.equals(lineCurrentState)) {
new Message(I18n.format("user.backpack.error_state", Bukkit.getOfflinePlayer(owner).getName(), lineNo, opener.getUniqueId())).broadcast(new Permission("nu.bp.admin"));
String errLine1 = String.format("%s's line %s (%d) changed when backpack opened by %s! ", owner.toString(), backpackLine.getId(), lineNo, opener.getUniqueId().toString());
String errLine2 = String.format("Should be %s, actually %s, desired %s", lineLastState, lineCurrentState, ItemStackUtils.itemsToBase64(line));
String errLine2 = String.format("Should be %s, actually %s, desired %s", lineLastState, lineCurrentState, desiredState);
Message message = new Message(errLine1 + "\n" + errLine2);
Bukkit.getOperators().forEach(op -> message.send(op, true));
plugin.getLogger().warning(errLine1);
plugin.getLogger().warning(errLine2);
query.rollback();
return false;
}
backpackLine.setItemStacks(line);
backpackLine.setItems(desiredState);
lastState.get(owner).set(backpackLine.getLineNo(), backpackLine.getItems());
plugin.getLogger().finer(() -> String.format("Saving %d: %s (%s)", lineNo, line, backpackLine.items));
plugin.getLogger().info(() -> String.format("Saving %d: %s (%s)", lineNo, line, backpackLine.getItems()));
query.update(backpackLine);
query.commit();
}
return true;
}

void onInventoryDrag(InventoryDragEvent event) {
if (maxLine > 5) {
int bottomLine = 5;
if (maxLine - 5 * (currentPage + 1) < 0) {
bottomLine = maxLine - currentPage * 5;
}
Set<Integer> bottomSlots = Stream.iterate(bottomLine * 9, i -> i + 1).limit(9).collect(Collectors.toSet());
Set<Integer> rawSlots = event.getRawSlots();
rawSlots.retainAll(bottomSlots);
if (rawSlots.size() != 0) {
event.setCancelled(true);
return;
}
}
Bukkit.getScheduler().runTask(plugin, () -> saveAll(event.getView().getTopInventory()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class ExtraBackpackLine {
public int lineNo;

@Column(name = "items", columnDefinition = "MEDIUMTEXT")
public String items;
private String items;

@Id
@Access(AccessType.PROPERTY)
Expand All @@ -35,6 +35,10 @@ public String getItems() {
return items;
}

public void setItems(String items) {
this.items = items;
}

public int getLineNo() {
return lineNo;
}
Expand Down Expand Up @@ -77,6 +81,6 @@ public void setItemStacks(List<ItemStack> itemStacks) {
if (itemStacks.size() != 9) {
throw new IllegalArgumentException("Invalid line given: " + itemStacks.size() + " items.");
}
items = ItemStackUtils.itemsToBase64(itemStacks);
setItems(ItemStackUtils.itemsToBase64(itemStacks));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.server.ServerEvent;

public class ExtraBackpackListener implements Listener {
private final NyaaUtils plugin;
Expand All @@ -27,7 +26,7 @@ public void onInventoryDrag(InventoryDragEvent event) {

@EventHandler
public void onInventoryClick(InventoryClickEvent event) {
if (event.getSlot() >= 0 && event.getSlot() == event.getRawSlot() && event.getInventory().getHolder() instanceof ExtraBackpackGUI) {
if (event.getInventory().getHolder() instanceof ExtraBackpackGUI) {
((ExtraBackpackGUI) event.getInventory().getHolder()).onInventoryClick(event);
}
}
Expand Down

0 comments on commit 765b583

Please sign in to comment.