2020import org .reflections .ReflectionUtils ;
2121import org .slf4j .Logger ;
2222import org .slf4j .LoggerFactory ;
23+ import org .terasology .config .facade .TelemetryConfiguration ;
2324import org .terasology .context .Context ;
2425import org .terasology .engine .subsystem .DisplayDevice ;
2526import org .terasology .module .sandbox .API ;
@@ -119,15 +120,14 @@ protected Map<String, Object> filterMetricMap(Map<String, Boolean> bindingMap) {
119120 TelemetryCategory telemetryCategory = this .getClass ().getAnnotation (TelemetryCategory .class );
120121 Context context = CoreRegistry .get (Context .class );
121122 DisplayDevice display = context .get (DisplayDevice .class );
122- if (display .isHeadless ()) {
123+ if (display .isHeadless () || telemetryCategory . isOneMapMetric () ) {
123124 return telemetryFieldToValue ;
124125 }
125126 Map <String , Object > metricMapAfterPermission = new HashMap <>();
126- for (Object key : telemetryFieldToValue .keySet ()) {
127- String fieldName = key .toString ();
128- String fieldNamewithID = telemetryCategory .id () + ":" + key .toString ();
129- if (bindingMap .containsKey (fieldNamewithID )) {
130- if (bindingMap .get (fieldNamewithID )) {
127+ for (String fieldName : telemetryFieldToValue .keySet ()) {
128+ String fieldNameWithID = telemetryCategory .id () + ":" + fieldName ;
129+ if (bindingMap .containsKey (fieldNameWithID )) {
130+ if (bindingMap .get (fieldNameWithID )) {
131131 metricMapAfterPermission .put (fieldName , telemetryFieldToValue .get (fieldName ));
132132 } else {
133133 metricMapAfterPermission .put (fieldName , "Disabled Field" );
@@ -138,6 +138,34 @@ protected Map<String, Object> filterMetricMap(Map<String, Boolean> bindingMap) {
138138 return metricMapAfterPermission ;
139139 }
140140
141+ /**
142+ * Filter the metric map by the binding map.
143+ * If the user doesn't want the field to be sent, its value will be covered by "Disabled Field".
144+ * This method could be used in module since {@link org.terasology.config.facade.TelemetryConfiguration} is exposed to modules
145+ * @param telemetryConfiguration the telemetry configuration exposed modules
146+ * @return a new metric map that covers the field that the user doesn't want to send by "Disabled Field".
147+ */
148+ protected Map <String , Object > filterMetricMap (TelemetryConfiguration telemetryConfiguration ) {
149+ TelemetryCategory telemetryCategory = this .getClass ().getAnnotation (TelemetryCategory .class );
150+ Context context = CoreRegistry .get (Context .class );
151+ DisplayDevice display = context .get (DisplayDevice .class );
152+ if (display .isHeadless () || telemetryCategory .isOneMapMetric ()) {
153+ return telemetryFieldToValue ;
154+ }
155+ Map <String , Object > metricMapAfterPermission = new HashMap <>();
156+ for (String fieldName : telemetryFieldToValue .keySet ()) {
157+ String fieldNameWithID = telemetryCategory .id () + ":" + fieldName ;
158+ if (telemetryConfiguration .containsField (fieldNameWithID )) {
159+ if (telemetryConfiguration .get (fieldNameWithID )) {
160+ metricMapAfterPermission .put (fieldName , telemetryFieldToValue .get (fieldName ));
161+ } else {
162+ metricMapAfterPermission .put (fieldName , "Disabled Field" );
163+ }
164+ }
165+ }
166+ return metricMapAfterPermission ;
167+ }
168+
141169 /**
142170 * Add the new metric to {@link org.terasology.telemetry.Metrics} instance.
143171 * This method will only be used when a metric constructor needs some specific arguments other than {@link org.terasology.context.Context}.
0 commit comments