Search in sources :

Example 1 with ScoreboardCriterion

use of net.minecraft.scoreboard.ScoreboardCriterion in project KahzerxMod by otakucraft.

the class ScoreboardExtension method showSideBar.

public void showSideBar(ServerCommandSource source, ItemStackArgument item, String type, boolean persistent) {
    Scoreboard scoreboard = source.getServer().getScoreboard();
    Item minecraftItem = item.getItem();
    String objectiveName = type + "." + Item.getRawId(minecraftItem);
    ScoreboardObjective scoreboardObjective = scoreboard.getNullableObjective(objectiveName);
    Entity entity = source.getEntity();
    Text text;
    if (scoreboardObjective != null) {
        if (scoreboard.getObjectiveForSlot(1) == scoreboardObjective) {
            text = MarkEnum.CROSS.appendMessage("Already showing");
        } else {
            assert entity != null;
            scoreboard.setObjectiveSlot(1, scoreboardObjective);
            if (persistent) {
                tickSet = -100;
            } else {
                tickSet = source.getServer().getTicks() + (20 * 20);
            }
            text = MarkEnum.TICK.appendText(new LiteralText(Formatting.WHITE + entity.getEntityName() + " has selected " + Formatting.GOLD + "[" + scoreboardObjective.getDisplayName().asString() + "]"));
        }
    } else {
        String criteriaName = "minecraft." + type + ":minecraft." + item.getItem().toString();
        String capitalize = type.substring(0, 1).toUpperCase() + type.substring(1);
        String displayName = capitalize + " " + minecraftItem.toString().replaceAll("_", " ");
        Optional<ScoreboardCriterion> opCriteria = ScoreboardCriterion.getOrCreateStatCriterion(criteriaName);
        if (opCriteria.isEmpty()) {
            return;
        }
        ScoreboardCriterion criteria = opCriteria.get();
        scoreboard.addObjective(objectiveName, criteria, new LiteralText(displayName).formatted(Formatting.GOLD), criteria.getDefaultRenderType());
        ScoreboardObjective newScoreboardObjective = scoreboardObjective = scoreboard.getNullableObjective(objectiveName);
        try {
            initScoreboard(source, newScoreboardObjective, minecraftItem, type);
        } catch (Exception e) {
            scoreboard.removeObjective(newScoreboardObjective);
            text = MarkEnum.CROSS.appendMessage("Error.");
            assert entity != null;
            source.getServer().getPlayerManager().broadcast(text, MessageType.CHAT, Util.NIL_UUID);
            return;
        }
        scoreboard.setObjectiveSlot(1, newScoreboardObjective);
        if (persistent) {
            tickSet = -100;
        } else {
            tickSet = source.getServer().getTicks() + (20 * 20);
        }
        assert entity != null;
        assert scoreboardObjective != null;
        text = MarkEnum.TICK.appendText(new LiteralText(Formatting.WHITE + entity.getEntityName() + " has selected " + Formatting.GOLD + "[" + scoreboardObjective.getDisplayName().asString() + "]"));
    }
    assert entity != null;
    source.getServer().getPlayerManager().broadcast(text, MessageType.CHAT, Util.NIL_UUID);
}
Also used : Item(net.minecraft.item.Item) Entity(net.minecraft.entity.Entity) ServerPlayerEntity(net.minecraft.server.network.ServerPlayerEntity) Scoreboard(net.minecraft.scoreboard.Scoreboard) ScoreboardObjective(net.minecraft.scoreboard.ScoreboardObjective) LiteralText(net.minecraft.text.LiteralText) Text(net.minecraft.text.Text) ScoreboardCriterion(net.minecraft.scoreboard.ScoreboardCriterion) LiteralText(net.minecraft.text.LiteralText)

Aggregations

Entity (net.minecraft.entity.Entity)1 Item (net.minecraft.item.Item)1 Scoreboard (net.minecraft.scoreboard.Scoreboard)1 ScoreboardCriterion (net.minecraft.scoreboard.ScoreboardCriterion)1 ScoreboardObjective (net.minecraft.scoreboard.ScoreboardObjective)1 ServerPlayerEntity (net.minecraft.server.network.ServerPlayerEntity)1 LiteralText (net.minecraft.text.LiteralText)1 Text (net.minecraft.text.Text)1