Search in sources :

Example 1 with TreeLayoutElement

use of org.spongepowered.api.advancement.TreeLayoutElement in project SpongeCommon by SpongePowered.

the class AdvancementTest method onGenerateTreeLayout.

@Listener
public void onGenerateTreeLayout(AdvancementTreeEvent.GenerateLayout event) {
    if (event.getTree() != this.advancementTree) {
        return;
    }
    this.logger.info("Updating advancement tree layout...");
    // Make the tree start at y 0, for every level within the tree
    // The min y position mapped by the used x positions
    // For example:
    // |- y
    // x -|- z
    // |- w
    // to
    // x -|- y
    // |- z
    // |- w
    final Map<Double, Double> values = new HashMap<>();
    for (TreeLayoutElement element : event.getLayout().getElements()) {
        final Vector2d pos = element.getPosition();
        if (!values.containsKey(pos.getX()) || pos.getY() < values.get(pos.getX())) {
            values.put(pos.getX(), pos.getY());
        }
    }
    for (TreeLayoutElement element : event.getLayout().getElements()) {
        final Vector2d pos = element.getPosition();
        element.setPosition(pos.getX(), pos.getY() - values.get(pos.getX()));
    }
/*
        // Rotate the advancement tree
        // The lines are currently drawn wrongly, that might be something
        // for later as it involves "tricking" the client
        double maxY = 0;
        for (TreeLayoutElement element : event.getLayout().getElements()) {
            maxY = Math.max(maxY, element.getPosition().getY());
        }
        for (TreeLayoutElement element : event.getLayout().getElements()) {
            final Vector2d pos = element.getPosition();
            element.setPosition(maxY - pos.getY(), pos.getX());
        }
        */
}
Also used : TreeLayoutElement(org.spongepowered.api.advancement.TreeLayoutElement) Vector2d(com.flowpowered.math.vector.Vector2d) HashMap(java.util.HashMap) Listener(org.spongepowered.api.event.Listener)

Example 2 with TreeLayoutElement

use of org.spongepowered.api.advancement.TreeLayoutElement in project LanternServer by LanternPowered.

the class LanternPlayerAdvancements method createAdvancement.

@SuppressWarnings("ConstantConditions")
private static MessagePlayOutAdvancements.AdvStruct createAdvancement(Locale locale, Advancement advancement) {
    final String parentId = advancement.getParent().map(Advancement::getId).orElse(null);
    final String background = parentId == null ? advancement.getTree().get().getBackgroundPath() : null;
    final DisplayInfo displayInfo = advancement.getDisplayInfo().orElse(null);
    final TreeLayoutElement layoutElement = ((LanternAdvancement) advancement).getLayoutElement();
    final String[][] criteriaRequirements = ((LanternAdvancement) advancement).clientCriteria.getSecond();
    final Set<String> criteria = new HashSet<>();
    for (String[] array : criteriaRequirements) {
        Collections.addAll(criteria, array);
    }
    return new MessagePlayOutAdvancements.AdvStruct(advancement.getId(), parentId, displayInfo == null ? null : createDisplay(locale, displayInfo, layoutElement, background), criteria, criteriaRequirements);
}
Also used : TreeLayoutElement(org.spongepowered.api.advancement.TreeLayoutElement) DisplayInfo(org.spongepowered.api.advancement.DisplayInfo) HashSet(java.util.HashSet)

Example 3 with TreeLayoutElement

use of org.spongepowered.api.advancement.TreeLayoutElement in project LanternServer by LanternPowered.

the class AdvancementTest method onGenerateTreeLayout.

@Listener
public void onGenerateTreeLayout(AdvancementTreeEvent.GenerateLayout event) {
    if (event.getTree() != this.advancementTree) {
        return;
    }
    this.logger.info("Updating advancement tree layout...");
    // Make the tree start at y 0, for every level within the tree
    // The min y position mapped by the used x positions
    // For example:
    // |- y
    // x -|- z
    // |- w
    // to
    // x -|- y
    // |- z
    // |- w
    final Map<Double, Double> values = new HashMap<>();
    for (TreeLayoutElement element : event.getLayout().getElements()) {
        final Vector2d pos = element.getPosition();
        if (!values.containsKey(pos.getX()) || pos.getY() < values.get(pos.getX())) {
            values.put(pos.getX(), pos.getY());
        }
    }
    for (TreeLayoutElement element : event.getLayout().getElements()) {
        final Vector2d pos = element.getPosition();
        element.setPosition(pos.getX(), pos.getY() - values.get(pos.getX()));
    }
/*
        // Rotate the advancement tree
        // The lines are currently drawn wrongly, that might be something
        // for later as it involves "tricking" the client
        double maxY = 0;
        for (TreeLayoutElement element : event.getLayout().getElements()) {
            maxY = Math.max(maxY, element.getPosition().getY());
        }
        for (TreeLayoutElement element : event.getLayout().getElements()) {
            final Vector2d pos = element.getPosition();
            element.setPosition(maxY - pos.getY(), pos.getX());
        }
        */
}
Also used : TreeLayoutElement(org.spongepowered.api.advancement.TreeLayoutElement) Vector2d(com.flowpowered.math.vector.Vector2d) HashMap(java.util.HashMap) Listener(org.spongepowered.api.event.Listener)

Example 4 with TreeLayoutElement

use of org.spongepowered.api.advancement.TreeLayoutElement in project LanternServer by LanternPowered.

the class LanternTreeLayout method generateNode.

private static TreeNode generateNode(Advancement advancement, TreeLayoutElement element, int column) {
    final TreeNode treeNode = new TreeNode(element, (double) column * HORIZONTAL_OFFSET);
    TreeNode previous = null;
    for (Advancement child : advancement.getChildren()) {
        final TreeLayoutElement childElement = ((LanternAdvancement) child).getLayoutElement();
        if (childElement != null) {
            final TreeNode childNode = generateNode(child, childElement, column + 1);
            childNode.previous = previous;
            childNode.parent = treeNode;
            if (previous != null) {
                previous.next = childNode;
            }
            treeNode.children.add(childNode);
            previous = childNode;
        }
    }
    return treeNode;
}
Also used : TreeLayoutElement(org.spongepowered.api.advancement.TreeLayoutElement) LanternAdvancement(org.lanternpowered.server.advancement.LanternAdvancement) LanternAdvancement(org.lanternpowered.server.advancement.LanternAdvancement) Advancement(org.spongepowered.api.advancement.Advancement)

Aggregations

TreeLayoutElement (org.spongepowered.api.advancement.TreeLayoutElement)4 Vector2d (com.flowpowered.math.vector.Vector2d)2 HashMap (java.util.HashMap)2 Listener (org.spongepowered.api.event.Listener)2 HashSet (java.util.HashSet)1 LanternAdvancement (org.lanternpowered.server.advancement.LanternAdvancement)1 Advancement (org.spongepowered.api.advancement.Advancement)1 DisplayInfo (org.spongepowered.api.advancement.DisplayInfo)1