/** * Creates a score component from the original to clone it. * * @param original the original for the new score component */ public ScoreComponent(ScoreComponent original) { super( original ); setName( original.getName() ); setObjective( original.getObjective() ); setValue( original.getValue() ); }
@Override public ScoreComponent duplicate() { return new ScoreComponent( this ); }
/** * Creates a new score component with the specified name and objective.<br> * If not specifically set, value will default to an empty string; * signifying that the scoreboard value should take precedence. If not null, * nor empty, {@code value} will override any value found in the * scoreboard.<br> * The value defaults to an empty string. * * @param name the name of the entity, or an entity selector, whose score * should be displayed * @param objective the internal name of the objective the entity's score is * attached to */ public ScoreComponent(String name, String objective) { setName( name ); setObjective( objective ); }
@Override public JsonElement serialize(ScoreComponent component, Type type, JsonSerializationContext context) { JsonObject root = new JsonObject(); serialize( root, component, context ); JsonObject json = new JsonObject(); json.addProperty( "name", component.getName() ); json.addProperty( "objective", component.getObjective() ); json.addProperty( "value", component.getValue() ); root.add( "score", json ); return root; } }
@Override public ScoreComponent deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject json = element.getAsJsonObject(); if ( !json.has( "name" ) || !json.has( "objective" ) ) { throw new JsonParseException( "A score component needs at least a name and an objective" ); } String name = json.get( "name" ).getAsString(); String objective = json.get( "objective" ).getAsString(); ScoreComponent component = new ScoreComponent( name, objective ); if ( json.has( "value" ) && !json.get( "value" ).getAsString().isEmpty() ) { component.setValue( json.get( "value" ).getAsString() ); } deserialize( json, component, context ); return component; }
/** * Transform a ScoreComponent by replacing the name and value with the * appropriate values. * * @param player the player to use for the component's name * @param component the component to transform */ private void transformScoreComponent(ProxiedPlayer player, ScoreComponent component) { Preconditions.checkArgument( !isSelectorPattern( component.getName() ), "Cannot transform entity selector patterns" ); if ( component.getValue() != null && !component.getValue().isEmpty() ) { return; // pre-defined values override scoreboard values } // check for '*' wildcard if ( component.getName().equals( "*" ) ) { component.setName( player.getName() ); } if ( player.getScoreboard().getObjective( component.getObjective() ) != null ) { Score score = player.getScoreboard().getScore( component.getName() ); if ( score != null ) { component.setValue( Integer.toString( score.getValue() ) ); } } }