/* * This file is part of NucleusFramework for Bukkit, licensed under the MIT License (MIT). * * Copyright (c) JCThePants (www.jcwhatever.com) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.jcwhatever.nucleus.utils.text.dynamic; import com.jcwhatever.nucleus.utils.PreCon; import com.jcwhatever.nucleus.utils.text.TextUtils; import com.jcwhatever.nucleus.utils.text.components.IChatMessage; /** * Dynamic text generated by {@link DynamicTextBuilder}. */ public final class DynamicTextComposite implements IDynamicText { private final String _template; private final IDynamicText[] _args; private final Object[] _formatArgs; private final int _interval; /** * Constructor. * * @param template Text template. * @param interval A hard set interval. 0 to use dynamic interval. * @param args Format arguments. */ DynamicTextComposite(String template, int interval, IDynamicText... args) { PreCon.notNull(template); PreCon.notNull(args); _template = template; _interval = interval; _args = args; _formatArgs = new Object[args.length]; System.arraycopy(args, 0, _formatArgs, 0, args.length); } @Override public IChatMessage nextText() { return TextUtils.format(_template, _formatArgs); } @Override public int getRefreshRate() { // check for interval hard set or no dynamic text if (_interval > 0 || _args.length == 0) return _interval; int interval = Integer.MAX_VALUE; // get smallest refresh rate required for (IDynamicText dyn : _args) { if (dyn.getRefreshRate() <= 0) continue; interval = Math.min(dyn.getRefreshRate(), interval); } // check for changed if (interval == Integer.MAX_VALUE) interval = 0; return interval; } @Override public String toString() { return nextText().toString(); } }