From d65323efb19f1fcf5aa83056ae0c0a91915b10a3 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 9 Jan 2015 15:46:23 +0100 Subject: [PATCH] More utilities and alterations to the progress bar system. --- .../massivecraft/massivecore/Progressbar.java | 83 +++++++++--- .../massivecraft/massivecore/util/MUtil.java | 122 ++++++++++++++++++ 2 files changed, 190 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/massivecraft/massivecore/Progressbar.java b/src/main/java/com/massivecraft/massivecore/Progressbar.java index 0c822af0..9f56106c 100644 --- a/src/main/java/com/massivecraft/massivecore/Progressbar.java +++ b/src/main/java/com/massivecraft/massivecore/Progressbar.java @@ -1,6 +1,8 @@ package com.massivecraft.massivecore; +import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -63,13 +65,13 @@ public class Progressbar public Progressbar withQuota(double quota) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } public Progressbar withWidth(int width) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } public Progressbar withLeft(String left) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar solid(String solid) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar between(String between) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar empty(String empty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar right(String right) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar solidsPerEmpty(double solidsPerEmpty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar colorTag(String colorTag) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - public Progressbar roofToColor(Map roofToColor) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withSolid(String solid) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withBetween(String between) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withEmpty(String empty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withRight(String right) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withSolidsPerEmpty(double solidsPerEmpty) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withColorTag(String colorTag) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } + public Progressbar withRoofToColor(Map roofToColor) { return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } // -------------------------------------------- // // PRIVATE CONSTRUCTOR @@ -106,13 +108,26 @@ public class Progressbar { return render(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); } - + + public List renderList() + { + return renderList(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); + } + // -------------------------------------------- // // STATIC UTIL // -------------------------------------------- // public static String render(double quota, int width, String left, String solid, String between, String empty, String right, double solidsPerEmpty, String colorTag, Map roofToColor) { + return Txt.implode(renderList(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor), ""); + } + + public static List renderList(double quota, int width, String left, String solid, String between, String empty, String right, double solidsPerEmpty, String colorTag, Map roofToColor) + { + // Create Ret + List ret = new ArrayList(); + // Ensure between 0 and 1; quota = limit(quota); @@ -125,18 +140,56 @@ public class Progressbar // The rest is empty int emptyCount = (int) ((width - solidCount) / solidsPerEmpty); - // Create the non-parsed bar - String ret = left + Txt.repeat(solid, solidCount) + between + Txt.repeat(empty, emptyCount) + right; + // Color Parse Parts + left = colorParse(left, colorTag, color); + solid = colorParse(solid, colorTag, color); + between = colorParse(between, colorTag, color); + empty = colorParse(empty, colorTag, color); + right = colorParse(right, colorTag, color); - // Replace color tag - ret = ret.replace(colorTag, color); - - // Parse amp color codes - ret = Txt.parse(ret); + // Combine Parts + if (left != null) + { + ret.add(left); + } + + if (solid != null) + { + for (int i = 1; i <= solidCount; i++) + { + ret.add(solid); + } + } + + if (between != null) + { + ret.add(between); + } + + if (empty != null) + { + for (int i = 1; i <= emptyCount; i++) + { + ret.add(empty); + } + } + + if (right != null) + { + ret.add(right); + } return ret; } + public static String colorParse(String string, String colorTag, String color) + { + if (string == null) return null; + string = string.replace(colorTag, color); + string = Txt.parse(string); + return string; + } + public static double limit(double quota) { if (quota > 1) return 1; diff --git a/src/main/java/com/massivecraft/massivecore/util/MUtil.java b/src/main/java/com/massivecraft/massivecore/util/MUtil.java index ddf8ea59..d8a7ee8a 100644 --- a/src/main/java/com/massivecraft/massivecore/util/MUtil.java +++ b/src/main/java/com/massivecraft/massivecore/util/MUtil.java @@ -292,6 +292,128 @@ public class MUtil return ret.toString(); } + // -------------------------------------------- // + // LIST OPERATIONS + // -------------------------------------------- // + + public static List repeat(T object, int times) + { + List ret = new ArrayList(times); + for (int i = 1; i <= times; i++) + { + ret.add(object); + } + return ret; + } + + public static void keepLeft(List list, int maxlength) + { + if (list.size() <= maxlength) return; + list.subList(maxlength, list.size()).clear(); + } + + public static void keepRight(List list, int maxlength) + { + if (list.size() <= maxlength) return; + list.subList(0, maxlength).clear(); + } + + public static void padLeft(List list, T object, int length) + { + if (list.size() >= length) return; + list.addAll(0, repeat(object, length - list.size())); + } + + public static void padRight(List list, T object, int length) + { + if (list.size() >= length) return; + list.addAll(repeat(object, length - list.size())); + } + + // -------------------------------------------- // + // ITERABLE MATH + // -------------------------------------------- // + + public static double getSum(Iterable numbers) + { + if (numbers == null) throw new NullPointerException("numbers"); + + double sum = 0; + for (T number : numbers) + { + sum += number.doubleValue(); + } + + return sum; + } + + public static double getAverage(Iterable numbers) + { + if (numbers == null) throw new NullPointerException("numbers"); + + double sum = 0; + int count = 0; + for (T number : numbers) + { + sum += number.doubleValue(); + count++; + } + + if (count == 0) throw new IllegalArgumentException("numbers empty"); + + return sum / count; + } + + // -------------------------------------------- // + // TABLE OPERATIONS + // -------------------------------------------- // + + public static List> rotateLeft(List> rows) + { + List> ret = transpose(rows); + flipVertically(ret); + return ret; + } + + public static List> rotateRight(List> rows) + { + List> ret = transpose(rows); + flipHorizontally(ret); + return ret; + } + + public static List> transpose(List> rows) + { + List> ret = new ArrayList>(); + + final int n = rows.get(0).size(); + + for (int i = 0; i < n; i++) + { + List col = new ArrayList(); + for (List row : rows) + { + col.add(row.get(i)); + } + ret.add(col); + } + + return ret; + } + + public static void flipHorizontally(List> rows) + { + for (List row : rows) + { + Collections.reverse(row); + } + } + + public static void flipVertically(List> rows) + { + Collections.reverse(rows); + } + // -------------------------------------------- // // COLOR INT CODE // -------------------------------------------- //