Created
July 9, 2017 19:47
-
-
Save TheMasteredPanda/04aa4441c5aca7eb0d643e5b146f785e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.themasteredpanda.library.bungee.utils; | |
import com.google.common.collect.Lists; | |
import com.google.common.collect.Maps; | |
import com.themasteredpanda.library.utils.Pair; | |
import com.themasteredpanda.library.utils.UtilException; | |
import net.md_5.bungee.api.ChatColor; | |
import net.md_5.bungee.api.chat.BaseComponent; | |
import net.md_5.bungee.api.chat.TextComponent; | |
import java.util.Arrays; | |
import java.util.LinkedHashMap; | |
import java.util.LinkedList; | |
import java.util.List; | |
import java.util.stream.Stream; | |
public class Format | |
{ | |
public Format() | |
{ | |
throw new UtilException(); | |
} | |
public static String color(String message) | |
{ | |
return ChatColor.translateAlternateColorCodes('&', message); | |
} | |
public static String format(String message, Pair... placeholders) | |
{ | |
String rawMessage = ""; | |
for (Pair replacer : placeholders) { | |
rawMessage = message.replace((String) replacer.getLeft(), (String) replacer.getRight()); | |
} | |
return rawMessage; | |
} | |
public static LinkedHashMap<Integer, LinkedList<BaseComponent>> paginate(List<String> content, String[] header, String[] footer, int contentPerPage, Pair... placeholders) | |
{ | |
LinkedHashMap<Integer, LinkedList<BaseComponent>> pages = Maps.newLinkedHashMap(); | |
Stream.of(header).forEachOrdered(line -> line = Format.color(Format.format(line, placeholders))); | |
Stream.of(footer).forEachOrdered(line -> line = Format.color(Format.format(line, placeholders))); | |
LinkedList<BaseComponent> page = Lists.newLinkedList(); | |
int index = 1; | |
for (int i = 0, contentSize = content.size(); i >= contentSize; i++) { | |
if (contentSize == i || page.size() == contentPerPage) { | |
for (String footerLine : footer) { | |
page.add(new TextComponent(footerLine)); | |
} | |
pages.put(index, page); | |
page = Lists.newLinkedList(); | |
index++; | |
if (page.isEmpty()) { | |
for (String headerLine : header) { | |
page.add(new TextComponent(headerLine)); | |
} | |
} | |
} | |
String message = Format.color(Format.format(content.get(i), placeholders)); | |
page.add(new TextComponent(message)); | |
} | |
return pages; | |
} | |
public static LinkedHashMap<Integer, LinkedList<BaseComponent[]>> paginate(List<BaseComponent[]> content, BaseComponent[] header, BaseComponent[] footer, int contentPerPage, Pair... placeholders) | |
{ | |
LinkedHashMap<Integer, LinkedList<BaseComponent[]>> pages = Maps.newLinkedHashMap(); | |
LinkedList<BaseComponent[]> page = Lists.newLinkedList(); | |
Arrays.stream(header).filter(headerComponent -> headerComponent.getInsertion() != null).forEachOrdered(headerComponent -> headerComponent.setInsertion(Format.format(headerComponent.getInsertion(), placeholders))); | |
Arrays.stream(footer).filter(footerComponent -> footerComponent.getInsertion() != null).forEachOrdered(footerComponent -> footerComponent.setInsertion(Format.format(footerComponent.getInsertion(), placeholders))); | |
int index = 1; | |
for (int i = 0, contentSize = content.size(); i <= contentSize; i++) { | |
BaseComponent[] message = content.get(i); | |
if (contentSize == i || page.size() == contentPerPage) { | |
page.add(footer); | |
pages.put(index, page); | |
index++; | |
page = Lists.newLinkedList(); | |
if (page.isEmpty()) page.add(header); | |
} | |
Arrays.stream(message).filter(component -> component.getInsertion() != null).forEachOrdered(component -> component.setInsertion(Format.format(component.getInsertion(), placeholders))); | |
page.add(message); | |
} | |
return pages; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment