|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Marvin (DerFrZocker) |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package de.derfrzocker.custom.ore.generator; |
| 26 | + |
| 27 | +import de.derfrzocker.custom.ore.generator.api.CustomData; |
| 28 | +import de.derfrzocker.custom.ore.generator.api.CustomOreGeneratorService; |
| 29 | +import de.derfrzocker.custom.ore.generator.api.OreConfig; |
| 30 | +import de.derfrzocker.custom.ore.generator.api.OreSetting; |
| 31 | +import org.apache.commons.lang.Validate; |
| 32 | +import org.bstats.bukkit.Metrics; |
| 33 | +import org.bukkit.Material; |
| 34 | +import org.bukkit.block.Biome; |
| 35 | +import org.bukkit.plugin.Plugin; |
| 36 | +import org.jetbrains.annotations.NotNull; |
| 37 | + |
| 38 | +import java.util.LinkedHashMap; |
| 39 | +import java.util.Map; |
| 40 | +import java.util.function.Supplier; |
| 41 | + |
| 42 | +public class CustomOreGeneratorMetrics { |
| 43 | + |
| 44 | + public CustomOreGeneratorMetrics(@NotNull final Plugin plugin, @NotNull final Supplier<CustomOreGeneratorService> serviceSupplier) { |
| 45 | + Validate.notNull(plugin, "Plugin can not be null"); |
| 46 | + Validate.notNull(serviceSupplier, "Service supplier can not be null"); |
| 47 | + |
| 48 | + final Metrics metrics = new Metrics(plugin); |
| 49 | + |
| 50 | + metrics.addCustomChart(new Metrics.SingleLineChart("used_in_worlds", () -> serviceSupplier.get().getWorldConfigs().size())); |
| 51 | + metrics.addCustomChart(new Metrics.SingleLineChart("created_ore_configs", () -> serviceSupplier.get().getOreConfigs().size())); |
| 52 | + metrics.addCustomChart(new Metrics.AdvancedPie("used_material", () -> { |
| 53 | + final Map<String, Integer> result = new LinkedHashMap<>(); |
| 54 | + |
| 55 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 56 | + final int count = result.getOrDefault(oreConfig.getMaterial().toString(), 0); |
| 57 | + |
| 58 | + result.put(oreConfig.getMaterial().toString(), count + 1); |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + })); |
| 63 | + |
| 64 | + metrics.addCustomChart(new Metrics.AdvancedPie("used_replace_material", () -> { |
| 65 | + final Map<String, Integer> result = new LinkedHashMap<>(); |
| 66 | + |
| 67 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 68 | + for (final Material material : oreConfig.getReplaceMaterials()) { |
| 69 | + final int count = result.getOrDefault(material.toString(), 0); |
| 70 | + |
| 71 | + result.put(material.toString(), count + 1); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return result; |
| 76 | + })); |
| 77 | + |
| 78 | + metrics.addCustomChart(new Metrics.AdvancedPie("used_select_material", () -> { |
| 79 | + final Map<String, Integer> result = new LinkedHashMap<>(); |
| 80 | + |
| 81 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 82 | + for (final Material material : oreConfig.getSelectMaterials()) { |
| 83 | + final int count = result.getOrDefault(material.toString(), 0); |
| 84 | + |
| 85 | + result.put(material.toString(), count + 1); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return result; |
| 90 | + })); |
| 91 | + |
| 92 | + metrics.addCustomChart(new Metrics.AdvancedPie("used_biomes", () -> { |
| 93 | + final Map<String, Integer> result = new LinkedHashMap<>(); |
| 94 | + |
| 95 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 96 | + for (final Biome biome : oreConfig.getBiomes()) { |
| 97 | + final int count = result.getOrDefault(biome.toString(), 0); |
| 98 | + |
| 99 | + result.put(biome.toString(), count + 1); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return result; |
| 104 | + })); |
| 105 | + |
| 106 | + metrics.addCustomChart(new Metrics.AdvancedPie("used_custom_data", () -> { |
| 107 | + final Map<String, Integer> result = new LinkedHashMap<>(); |
| 108 | + |
| 109 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 110 | + for (final CustomData customData : oreConfig.getCustomData().keySet()) { |
| 111 | + final int count = result.getOrDefault(customData.getName(), 0); |
| 112 | + |
| 113 | + result.put(customData.getName(), count + 1); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return result; |
| 118 | + })); |
| 119 | + |
| 120 | + metrics.addCustomChart(new Metrics.DrilldownPie("used_ore_generator", () -> { |
| 121 | + final Map<String, Map<String, Integer>> result = new LinkedHashMap<>(); |
| 122 | + |
| 123 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 124 | + final String oreGenerator = oreConfig.getOreGenerator(); |
| 125 | + final Map<String, Integer> settingsMap = result.computeIfAbsent(oreGenerator, name -> new LinkedHashMap<>()); |
| 126 | + |
| 127 | + settingsMap.put("used", settingsMap.getOrDefault("used", 0) + 1); |
| 128 | + |
| 129 | + for (final OreSetting oreSetting : oreConfig.getOreGeneratorOreSettings().getValues().keySet()) { |
| 130 | + final int count = settingsMap.getOrDefault(oreSetting.getName(), 0); |
| 131 | + |
| 132 | + settingsMap.put(oreSetting.getName(), count + 1); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + return result; |
| 137 | + })); |
| 138 | + |
| 139 | + metrics.addCustomChart(new Metrics.DrilldownPie("used_block_selector", () -> { |
| 140 | + final Map<String, Map<String, Integer>> result = new LinkedHashMap<>(); |
| 141 | + |
| 142 | + for (final OreConfig oreConfig : serviceSupplier.get().getOreConfigs()) { |
| 143 | + final String blockSelector = oreConfig.getBlockSelector(); |
| 144 | + final Map<String, Integer> settingsMap = result.computeIfAbsent(blockSelector, name -> new LinkedHashMap<>()); |
| 145 | + |
| 146 | + settingsMap.put("used", settingsMap.getOrDefault("used", 0) + 1); |
| 147 | + |
| 148 | + for (final OreSetting oreSetting : oreConfig.getBlockSelectorOreSettings().getValues().keySet()) { |
| 149 | + final int count = settingsMap.getOrDefault(oreSetting.getName(), 0); |
| 150 | + |
| 151 | + settingsMap.put(oreSetting.getName(), count + 1); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return result; |
| 156 | + })); |
| 157 | + |
| 158 | + if (!metrics.isEnabled()) { |
| 159 | + plugin.getLogger().warning("meh"); |
| 160 | + } |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | +} |
0 commit comments