Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advancements API #1554

Merged
merged 15 commits into from
Dec 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ task('setupDecompWorkspace', dependsOn: 'genEventImpl')

sortClassFields {
add 'main', 'org.spongepowered.api.CatalogTypes'
add 'main', 'org.spongepowered.api.advancement.AdvancementTypes'
add 'main', 'org.spongepowered.api.advancement.criteria.trigger.Triggers'
add 'main', 'org.spongepowered.api.boss.BossBarColors'
add 'main', 'org.spongepowered.api.boss.BossBarOverlays'
add 'main', 'org.spongepowered.api.data.key.Keys'
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/spongepowered/api/CatalogTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
package org.spongepowered.api;

import org.spongepowered.api.advancement.Advancement;
import org.spongepowered.api.advancement.AdvancementTree;
import org.spongepowered.api.advancement.AdvancementType;
import org.spongepowered.api.advancement.criteria.trigger.Trigger;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.api.block.tileentity.TileEntityType;
import org.spongepowered.api.boss.BossBarColor;
Expand Down Expand Up @@ -95,6 +99,12 @@ public final class CatalogTypes {

// SORTFIELDS:ON

public static final Class<Advancement> ADVANCEMENT = Advancement.class;

public static final Class<AdvancementTree> ADVANCEMENT_TREE = AdvancementTree.class;

public static final Class<AdvancementType> ADVANCEMENT_TYPE = AdvancementType.class;

public static final Class<AITaskType> AI_TASK_TYPE = AITaskType.class;

public static final Class<ArmorType> ARMOR_TYPE = ArmorType.class;
Expand Down Expand Up @@ -295,6 +305,8 @@ public final class CatalogTypes {

public static final Class<TreeType> TREE_TYPE = TreeType.class;

public static final Class<Trigger> TRIGGER = Trigger.class;

public static final Class<Visibility> VISIBILITY = Visibility.class;

public static final Class<WallType> WALL_TYPE = WallType.class;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/spongepowered/api/GameRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.spongepowered.api.entity.ai.task.AITaskType;
import org.spongepowered.api.entity.ai.task.AbstractAITask;
import org.spongepowered.api.entity.living.Agent;
import org.spongepowered.api.event.game.GameRegistryEvent;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.merchant.TradeOfferGenerator;
import org.spongepowered.api.item.merchant.VillagerRegistry;
Expand Down Expand Up @@ -222,7 +223,10 @@ <T extends CatalogType> GameRegistry registerModule(Class<T> catalogClass, Catal
* is not supported
* @throws CatalogTypeAlreadyRegisteredException if the type cannot be
* registered because a matching type was already registered
* @deprecated Is scheduled to be removed in API 8, the
* {@link GameRegistryEvent.Register} should be used instead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested classes will not link properly in Java 8's javadoc generator. They need to be fully qualified to work.

*/
@Deprecated
<T extends CatalogType> T register(Class<T> type, T obj) throws IllegalArgumentException, CatalogTypeAlreadyRegisteredException;

/**
Expand Down
167 changes: 167 additions & 0 deletions src/main/java/org/spongepowered/api/advancement/Advancement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* 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 org.spongepowered.api.advancement;

import org.spongepowered.api.CatalogType;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.advancement.criteria.AdvancementCriterion;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.TextRepresentable;
import org.spongepowered.api.util.ResettableBuilder;

import java.util.Collection;
import java.util.List;
import java.util.Optional;

import javax.annotation.Nullable;

/**
* An advancement.
*/
public interface Advancement extends CatalogType, TextRepresentable {

/**
* Creates a new {@link Builder} to create {@link Advancement}s.
*
* @return The new builder
*/
static Builder builder() {
return Sponge.getRegistry().createBuilder(Builder.class);
}

/**
* Gets the {@link AdvancementTree} this advancement is located in,
* will only be present if the root advancement was used to create
* a {@link AdvancementTree}.
*
* @return The advancement tree
*/
Optional<AdvancementTree> getTree();

/**
* Gets all the children {@link Advancement}s.
*
* @return The children advancements
*/
Collection<Advancement> getChildren();

/**
* Gets all the {@link AdvancementCriterion} that should be achieved
* before this advancement is unlocked. {@link AdvancementCriterion#EMPTY}
* will be returned if no criterion was assigned.
* <p>
* This {@link AdvancementCriterion} can be a AND or OR operation that
* contains multiple possible {@link AdvancementCriterion}s.
*
* @return The criterion
*/
AdvancementCriterion getCriterion();

/**
* Gets the parent {@link Advancement}, if present.
*
* @return The parent advancement, if present
*/
Optional<Advancement> getParent();

/**
* Gets the {@link DisplayInfo} of this advancement, if present.
*
* @return The display info, if present
*/
Optional<DisplayInfo> getDisplayInfo();

/**
* Gets the {@link Text} lines that would be used to
* display a toast notification.
*
* @return The text lines
*/
List<Text> toToastText();

/**
* A builder to create {@link Advancement}s.
*/
interface Builder extends ResettableBuilder<Advancement, Builder> {

/**
* Sets the parent {@link Advancement}. Defaults to {code null}.
*
* @param parent The parent advancement
* @return This builder, for chaining
*/
Builder parent(@Nullable Advancement parent);

/**
* Sets the {@link AdvancementCriterion} that should be used
* for the advancement. Defaults to {@link AdvancementCriterion#EMPTY}.
*
* @param criterion The criterion
* @return This builder, for chaining
*/
Builder criterion(AdvancementCriterion criterion);

/**
* Sets the {@link DisplayInfo}. Defaults to {code null}.
*
* @param displayInfo The display info
* @return This builder, for chaining
*/
Builder displayInfo(@Nullable DisplayInfo displayInfo);

/**
* Sets the identifier of the {@link Advancement}
* (without the namespace).
*
* @param id The identifier
* @return This builder, for chaining
*/
Builder id(String id);

/**
* Sets the name of the {@link Advancement}. Defaults to
* the plain {@link DisplayInfo#getTitle()} if the
* {@link DisplayInfo} is present. Otherwise will it default
* to the identifier ({@link #id(String)}).
*
* @param name The name
* @return This builder, for chaining
*/
Builder name(String name);

/**
* Builds the {@link Advancement}.
*
* @return The advancement
*/
Advancement build();

@Override
@Deprecated
default Builder from(Advancement value) throws UnsupportedOperationException {
throw new UnsupportedOperationException("Cannot create duplicate advancements!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* 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 org.spongepowered.api.advancement;

import org.spongepowered.api.advancement.criteria.AdvancementCriterion;
import org.spongepowered.api.advancement.criteria.CriterionProgress;
import org.spongepowered.api.advancement.criteria.ScoreAdvancementCriterion;
import org.spongepowered.api.advancement.criteria.ScoreCriterionProgress;

import java.util.Optional;

/**
* Represents the progress of a {@link Advancement}.
*/
public interface AdvancementProgress extends Progressable {

/**
* Gets the {@link Advancement}.
*
* @return The advancement
*/
Advancement getAdvancement();

/**
* Gets the {@link ScoreCriterionProgress} for the given {@link ScoreAdvancementCriterion}.
*
* @param criterion The score criterion
* @return The score criterion progress
*/
default ScoreCriterionProgress require(ScoreAdvancementCriterion criterion) {
return get(criterion).orElseThrow(() -> new IllegalStateException("The score criterion " + criterion.getName() +
" isn't present on the advancement " + getAdvancement().getName()));
}

/**
* Gets the {@link ScoreCriterionProgress} for the given {@link ScoreAdvancementCriterion},
* this will only be returned of the criterion is present on the {@link Advancement}.
*
* @param criterion The score criterion
* @return The score criterion progress
*/
Optional<ScoreCriterionProgress> get(ScoreAdvancementCriterion criterion);

/**
* Gets the {@link CriterionProgress} for the given {@link AdvancementCriterion}.
* <p>
* For AND and OR criteria will wrapped {@link CriterionProgress} be provided that will
* interact with the {@link CriterionProgress}s for every child {@link AdvancementCriterion}s.
*
* @param criterion The criterion
* @return The criterion progress
*/
default CriterionProgress require(AdvancementCriterion criterion) {
return get(criterion).orElseThrow(() -> new IllegalStateException("The criterion " + criterion.getName() +
" isn't present on the advancement " + getAdvancement().getName()));
}

/**
* Gets the {@link CriterionProgress} for the given {@link AdvancementCriterion},
* this will only be returned of the criterion is present on the {@link Advancement}.
* <p>
* For AND and OR criteria will wrapped {@link CriterionProgress} be provided that will
* interact with the {@link CriterionProgress}s for every child {@link AdvancementCriterion}s.
*
* @param criterion The criterion
* @return The criterion progress
*/
Optional<CriterionProgress> get(AdvancementCriterion criterion);
}