Skip to content

Commit

Permalink
2.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mdaffa48 committed Nov 8, 2024
1 parent 6b84b4e commit 45dbe0d
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
4 changes: 2 additions & 2 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.muhammaddaffa</groupId>
<artifactId>MDLib</artifactId>
<name>MDLib</name>
<version>2.0.0-beta7</version>
<version>2.0.1</version>
<description>A library for plugin that was created for helping spigot plugin development</description>
<url>https://muhammaddaffa.com</url>
<build>
Expand Down Expand Up @@ -128,7 +128,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.21-R0.1-SNAPSHOT</version>
<version>1.21.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.muhammaddaffa</groupId>
<artifactId>MDLib</artifactId>
<version>2.0.1</version>
<version>2.0.2</version>

<name>MDLib</name>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.muhammaddaffa.mdlib.gui.pagination;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Pagination {

private final Map<Integer, List<?>> pageItems = new HashMap<>();

public int totalPage = 1;
public int currentPage = 1;

public Pagination(List<?> items, int maxItemsPerPage) {
if (items == null || maxItemsPerPage <= 0) {
throw new IllegalArgumentException("Invalid input list or maxItemsPerPage");
}

// If the items is empty
if (items.isEmpty()) {
pageItems.put(1, new ArrayList<>());
return;
}

int totalItems = items.size();
totalPage = (int) Math.ceil((double) totalItems / maxItemsPerPage);

for (int i = 0; i < totalPage; i++) {
int start = i * maxItemsPerPage;
int end = Math.min(start + maxItemsPerPage, totalItems);
List<?> pageContent = items.subList(start, end);
pageItems.put(i + 1, new ArrayList<>(pageContent));
}
}

public boolean nextPage() {
return this.pageItems.get(this.currentPage + 1) != null;
}

public boolean previousPage() {
return this.pageItems.get(this.currentPage - 1) != null;
}

public List<?> getItems() {
return this.pageItems.get(this.currentPage);
}

}
30 changes: 25 additions & 5 deletions src/main/java/com/muhammaddaffa/mdlib/utils/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -150,16 +153,33 @@ public static boolean isInt(String s) {
}

public static void addInventoryItem(Player player, ItemStack stack) {
player.getInventory().addItem(stack).forEach((integer, item) -> {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
int amount = stack.getAmount();
Material material = stack.getType();
int maxStackSize = material.getMaxStackSize();

Map<Integer, ItemStack> leftovers = new HashMap<>();

while (amount > 0) {
int stackAmount = Math.min(amount, maxStackSize);
ItemStack stackToAdd = new ItemStack(material, stackAmount);

Map<Integer, ItemStack> left = player.getInventory().addItem(stackToAdd);
if (!left.isEmpty()) {
leftovers.putAll(left);
}

amount -= stackAmount;
}

// Drop any leftovers
leftovers.values().forEach(item -> {
player.getWorld().dropItemNaturally(player.getLocation(), item);
});
}

public static void addInventoryItem(Player player, List<ItemStack> items) {
for (ItemStack stack : items) {
player.getInventory().addItem(stack).forEach((integer, item) -> {
player.getWorld().dropItemNaturally(player.getLocation(), stack);
});
addInventoryItem(player, stack);
}
}

Expand Down

0 comments on commit 45dbe0d

Please sign in to comment.