From 9b3a87c0da525edeebd6fc49b776f1a36bea7853 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Fri, 26 Apr 2013 17:54:53 +0200 Subject: [PATCH] Add a progressbar system --- src/com/massivecraft/mcore/Progressbar.java | 163 ++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 src/com/massivecraft/mcore/Progressbar.java diff --git a/src/com/massivecraft/mcore/Progressbar.java b/src/com/massivecraft/mcore/Progressbar.java new file mode 100644 index 00000000..9fc69136 --- /dev/null +++ b/src/com/massivecraft/mcore/Progressbar.java @@ -0,0 +1,163 @@ +package com.massivecraft.mcore; + +import java.util.Collections; +import java.util.Map; +import java.util.Map.Entry; + +import com.massivecraft.mcore.util.MUtil; +import com.massivecraft.mcore.util.Txt; + +public class Progressbar +{ + // -------------------------------------------- // + // STANDARD INSTANCES + // -------------------------------------------- // + + public static final transient Progressbar HEALTHBAR_CLASSIC = Progressbar.valueOf(1D, 30, "{c}[", "|", "&8", "|", "{c}]", 1D, "{c}", MUtil.map( + 1.0D, "&2", + 0.8D, "&a", + 0.5D, "&e", + 0.4D, "&6", + 0.3D, "&c", + 0.2D, "&4" + )); + + // -------------------------------------------- // + // FIELDS: RAW + // -------------------------------------------- // + + private final double quota; + public double getQuota() { return this.quota; } + + private final int width; + public int getWidth() { return this.width; } + + private final String left; + public String getLeft() { return this.left; } + + private final String solid; + public String getSolid() { return this.solid; } + + private final String between; + public String getBetween() { return this.between; } + + private final String empty; + public String getEmpty() { return this.empty; } + + private final String right; + public String getRight() { return this.right; } + + private final double solidsPerEmpty; + public double getSolidsPerEmpty() { return this.solidsPerEmpty; } + + private final String colorTag; + public String getColorTag() { return this.colorTag; } + + private final Map roofToColor; + public Map getRoofToColor() { return this.roofToColor; } + + // -------------------------------------------- // + // FIELDS: WITH + // -------------------------------------------- // + + 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); } + + // -------------------------------------------- // + // PRIVATE CONSTRUCTOR + // -------------------------------------------- // + + private Progressbar(double quota, int width, String left, String solid, String between, String empty, String right, double solidsPerEmpty, String colorTag, Map roofToColor) + { + this.quota = quota; + this.width = width; + this.left = left; + this.solid = solid; + this.between = between; + this.empty = empty; + this.right = right; + this.solidsPerEmpty = solidsPerEmpty; + this.colorTag = colorTag; + this.roofToColor = Collections.unmodifiableMap(roofToColor); + } + + // -------------------------------------------- // + // FACTORY: VALUE OF + // -------------------------------------------- // + + public static Progressbar valueOf(double quota, int width, String left, String solid, String between, String empty, String right, double solidsPerEmpty, String colorTag, Map roofToColor) + { + return new Progressbar(quota, width, left, solid, between, empty, right, solidsPerEmpty, colorTag, roofToColor); + } + + // -------------------------------------------- // + // INSTANCE METHODS + // -------------------------------------------- // + + public String render() + { + return render(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) + { + // Ensure between 0 and 1; + quota = limit(quota); + + // What color is the health bar? + String color = pick(quota, roofToColor); + + // how much solid should there be? + int solidCount = (int) Math.ceil(width * quota); + + // 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; + + // Replace color tag + ret = ret.replace(colorTag, color); + + // Parse amp color codes + ret = Txt.parse(ret); + + return ret; + } + + public static double limit(double quota) + { + if (quota > 1) return 1; + if (quota < 0) return 0; + return quota; + } + + public static T pick(double quota, Map roofToValue) + { + Double currentRoof = null; + T ret = null; + for (Entry entry : roofToValue.entrySet()) + { + double roof = entry.getKey(); + T value = entry.getValue(); + if (quota <= roof && (currentRoof == null || roof <= currentRoof)) + { + currentRoof = roof; + ret = value; + } + } + return ret; + } +}