Search in sources :

Example 1 with AbstractPerk

use of hellfirepvp.astralsorcery.common.perk.AbstractPerk in project AstralSorcery by HellFirePvP.

the class ResearchManager method removeAllAllocatedPerks.

private static void removeAllAllocatedPerks(PlayerProgress progress, PlayerEntity player) {
    PlayerPerkData perkData = progress.getPerkData();
    List<AbstractPerk> allocatedPerks = new ArrayList<>(perkData.getAllocatedPerks(PerkAllocationType.UNLOCKED));
    List<AbstractPerk> syncRemovable = new ArrayList<>();
    for (AbstractPerk perk : allocatedPerks) {
        if (doRemovePerk(progress, player, LogicalSide.SERVER, perk, PlayerPerkAllocation.unlock(), false)) {
            syncRemovable.add(perk);
        }
    }
    List<ResourceLocation> removals = syncRemovable.stream().map(AbstractPerk::getRegistryName).collect(Collectors.toList());
    PacketChannel.CHANNEL.sendToPlayer(player, new PktSyncPerkActivity(removals));
}
Also used : AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) ResourceLocation(net.minecraft.util.ResourceLocation) PktSyncPerkActivity(hellfirepvp.astralsorcery.common.network.play.server.PktSyncPerkActivity)

Example 2 with AbstractPerk

use of hellfirepvp.astralsorcery.common.perk.AbstractPerk in project AstralSorcery by HellFirePvP.

the class PlayerPerkData method load.

void load(PlayerProgress progress, CompoundNBT tag) {
    this.perks.clear();
    this.freePointTokens.clear();
    this.perkExp = 0;
    // TODO remove with 1.17
    if (isLegacyData(tag)) {
        loadLegacyData(progress, tag);
        return;
    }
    this.perkExp = tag.getDouble("perkExp");
    long perkTreeVersion = tag.getLong("perkTreeVersion");
    if (PerkTree.PERK_TREE.getVersion(LogicalSide.SERVER).map(v -> !v.equals(perkTreeVersion)).orElse(true)) {
        // If your perk tree is different, clear it.
        AstralSorcery.log.info("Clearing perk-tree because the player's skill-tree version was outdated!");
        if (progress.getAttunedConstellation() != null) {
            AbstractPerk root = PerkTree.PERK_TREE.getRootPerk(LogicalSide.SERVER, progress.getAttunedConstellation());
            if (root != null) {
                AppliedPerk newPerk = new AppliedPerk(root);
                newPerk.addAllocation(PlayerPerkAllocation.unlock(), false);
                root.onUnlockPerkServer(null, PerkAllocationType.UNLOCKED, progress, newPerk.getPerkData());
                this.perks.put(root, newPerk);
            }
        }
        return;
    }
    // TODO Remove .replace("-", "_") in 1.17
    this.freePointTokens.addAll(NBTHelper.readList(tag, "tokens", Constants.NBT.TAG_STRING, nbt -> new ResourceLocation(nbt.getString().replace("-", "_"))));
    ListNBT list = tag.getList("perks", Constants.NBT.TAG_COMPOUND);
    for (int i = 0; i < list.size(); i++) {
        CompoundNBT nbt = list.getCompound(i);
        AppliedPerk.deserialize(nbt).ifPresent(perk -> this.perks.put(perk.getPerk(), perk));
    }
}
Also used : PerkLevelManager(hellfirepvp.astralsorcery.common.perk.PerkLevelManager) ListNBT(net.minecraft.nbt.ListNBT) StringNBT(net.minecraft.nbt.StringNBT) IntStream(java.util.stream.IntStream) ByteBufUtils(hellfirepvp.astralsorcery.common.util.data.ByteBufUtils) java.util(java.util) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) PlayerEntity(net.minecraft.entity.player.PlayerEntity) NBTHelper(hellfirepvp.astralsorcery.common.util.nbt.NBTHelper) Predicate(java.util.function.Predicate) Constants(net.minecraftforge.common.util.Constants) CompoundNBT(net.minecraft.nbt.CompoundNBT) AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) Collectors(java.util.stream.Collectors) Dist(net.minecraftforge.api.distmarker.Dist) PktSyncKnowledge(hellfirepvp.astralsorcery.common.network.play.server.PktSyncKnowledge) PerkTree(hellfirepvp.astralsorcery.common.perk.PerkTree) AstralSorcery(hellfirepvp.astralsorcery.AstralSorcery) MathHelper(net.minecraft.util.math.MathHelper) ResourceLocation(net.minecraft.util.ResourceLocation) MapStream(hellfirepvp.astralsorcery.common.util.MapStream) PacketBuffer(net.minecraft.network.PacketBuffer) Nullable(javax.annotation.Nullable) LogicalSide(net.minecraftforge.fml.LogicalSide) AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) ListNBT(net.minecraft.nbt.ListNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 3 with AbstractPerk

use of hellfirepvp.astralsorcery.common.perk.AbstractPerk in project AstralSorcery by HellFirePvP.

the class PktSyncPerkActivity method handler.

@Nonnull
@Override
public Handler<PktSyncPerkActivity> handler() {
    return new Handler<PktSyncPerkActivity>() {

        @Override
        @OnlyIn(Dist.CLIENT)
        public void handleClient(PktSyncPerkActivity packet, NetworkEvent.Context context) {
            context.enqueueWork(() -> {
                PlayerEntity player = Minecraft.getInstance().player;
                if (player == null) {
                    return;
                }
                switch(packet.type) {
                    case CLEARALL:
                        PerkEffectHelper.clientClearAllPerks();
                        break;
                    case REMOVE_LISTED:
                        List<AbstractPerk> perks = packet.perkKeys.stream().map(key -> PerkTree.PERK_TREE.getPerk(LogicalSide.CLIENT, key)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
                        PerkEffectHelper.modifySources(player, LogicalSide.CLIENT, perks, PerkEffectHelper.Action.REMOVE);
                        break;
                    case UNLOCKALL:
                        PerkEffectHelper.clientRefreshAllPerks();
                        break;
                    case DATACHANGE:
                        PerkTree.PERK_TREE.getPerk(LogicalSide.CLIENT, packet.perkKey).ifPresent(perk -> {
                            PerkEffectHelper.clientChangePerkData(perk, packet.oldData, packet.newData);
                        });
                        break;
                    default:
                        break;
                }
            });
        }

        @Override
        public void handle(PktSyncPerkActivity packet, NetworkEvent.Context context, LogicalSide side) {
        }
    };
}
Also used : ByteBufUtils(hellfirepvp.astralsorcery.common.util.data.ByteBufUtils) OnlyIn(net.minecraftforge.api.distmarker.OnlyIn) PlayerEntity(net.minecraft.entity.player.PlayerEntity) PerkEffectHelper(hellfirepvp.astralsorcery.common.perk.PerkEffectHelper) Collection(java.util.Collection) CompoundNBT(net.minecraft.nbt.CompoundNBT) NetworkEvent(net.minecraftforge.fml.network.NetworkEvent) AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) Collectors(java.util.stream.Collectors) Dist(net.minecraftforge.api.distmarker.Dist) ArrayList(java.util.ArrayList) PerkTree(hellfirepvp.astralsorcery.common.perk.PerkTree) List(java.util.List) Minecraft(net.minecraft.client.Minecraft) ResourceLocation(net.minecraft.util.ResourceLocation) Optional(java.util.Optional) ASPacket(hellfirepvp.astralsorcery.common.network.base.ASPacket) Nonnull(javax.annotation.Nonnull) LogicalSide(net.minecraftforge.fml.LogicalSide) AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) Optional(java.util.Optional) LogicalSide(net.minecraftforge.fml.LogicalSide) PlayerEntity(net.minecraft.entity.player.PlayerEntity) Nonnull(javax.annotation.Nonnull)

Example 4 with AbstractPerk

use of hellfirepvp.astralsorcery.common.perk.AbstractPerk in project AstralSorcery by HellFirePvP.

the class PerkTreeLoader method loadPerkTree.

public static PerkTreeData loadPerkTree(Collection<JsonObject> perkTreeObjects) {
    PerkTreeData newTree = new PerkTreeData();
    int count = 0;
    for (JsonObject serializedPerkData : perkTreeObjects) {
        ResourceLocation perkRegistryName = new ResourceLocation(JSONUtils.getString(serializedPerkData, "registry_name"));
        ResourceLocation customClass = PerkTypeHandler.DEFAULT.getKey();
        if (serializedPerkData.has("perk_class")) {
            customClass = new ResourceLocation(JSONUtils.getString(serializedPerkData, "perk_class"));
            if (!PerkTypeHandler.hasCustomType(customClass)) {
                throw new JsonParseException("Unknown perk_class: " + customClass.toString());
            }
        }
        float posX = JSONUtils.getFloat(serializedPerkData, "x");
        float posY = JSONUtils.getFloat(serializedPerkData, "y");
        AbstractPerk perk = PerkTypeHandler.convert(perkRegistryName, posX, posY, customClass);
        if (serializedPerkData.has("name")) {
            String name = JSONUtils.getString(serializedPerkData, "name");
            perk.setName(name);
        }
        if (serializedPerkData.has("hiddenUnlessAllocated")) {
            perk.setHiddenUnlessAllocated(JSONUtils.getBoolean(serializedPerkData, "hiddenUnlessAllocated"));
        }
        if (serializedPerkData.has("data")) {
            JsonObject perkData = JSONUtils.getJsonObject(serializedPerkData, "data");
            perk.deserializeData(perkData);
        }
        LoadedPerkData connector = newTree.addPerk(perk, serializedPerkData);
        if (serializedPerkData.has("connection")) {
            JsonArray connectionArray = JSONUtils.getJsonArray(serializedPerkData, "connection");
            for (int i = 0; i < connectionArray.size(); i++) {
                JsonElement connection = connectionArray.get(i);
                String connectedPerkKey = JSONUtils.getString(connection, String.format("connection[%s]", i));
                connector.addConnection(new ResourceLocation(connectedPerkKey));
            }
        }
        count++;
    }
    AstralSorcery.log.info("Built PerkTree with {} perks!", count);
    return newTree;
}
Also used : AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) ResourceLocation(net.minecraft.util.ResourceLocation)

Example 5 with AbstractPerk

use of hellfirepvp.astralsorcery.common.perk.AbstractPerk in project AstralSorcery by HellFirePvP.

the class GemSocketPerk method setContainedItem.

public default <T extends AbstractPerk & GemSocketPerk> boolean setContainedItem(PlayerEntity player, LogicalSide side, @Nullable CompoundNBT dataOvr, ItemStack stack) {
    if (!(this instanceof AbstractPerk)) {
        throw new UnsupportedOperationException("Cannot do perk-specific socketing logic on something that's not a perk!");
    }
    T thisPerk = (T) this;
    PlayerProgress prog = ResearchHelper.getProgress(player, side);
    if (!prog.getPerkData().hasPerkEffect(thisPerk)) {
        return false;
    }
    // a given data override signifies that that override should be used, but not written.
    boolean useLiveData = dataOvr == null;
    CompoundNBT data = dataOvr;
    if (useLiveData) {
        data = ((AbstractPerk) this).getPerkData(player, side);
    }
    if (data == null) {
        return false;
    }
    CompoundNBT prev = data.copy();
    if (stack.isEmpty()) {
        ItemStack existing = NBTHelper.getStack(data, SOCKET_DATA_KEY);
        if (!existing.isEmpty() && existing.getItem() instanceof GemSocketItem) {
            ((GemSocketItem) existing.getItem()).onExtract(existing, thisPerk, player, prog);
        }
        data.remove(SOCKET_DATA_KEY);
    } else {
        if (stack.getItem() instanceof GemSocketItem) {
            ((GemSocketItem) stack.getItem()).onInsert(stack, thisPerk, player, prog);
        }
        NBTHelper.setStack(data, SOCKET_DATA_KEY, stack);
    }
    if (useLiveData) {
        ResearchManager.setPerkData(player, thisPerk, prev, data);
    }
    return true;
}
Also used : AbstractPerk(hellfirepvp.astralsorcery.common.perk.AbstractPerk) CompoundNBT(net.minecraft.nbt.CompoundNBT) CompoundNBT(net.minecraft.nbt.CompoundNBT) ItemStack(net.minecraft.item.ItemStack) PlayerProgress(hellfirepvp.astralsorcery.common.data.research.PlayerProgress)

Aggregations

AbstractPerk (hellfirepvp.astralsorcery.common.perk.AbstractPerk)14 CompoundNBT (net.minecraft.nbt.CompoundNBT)8 ResourceLocation (net.minecraft.util.ResourceLocation)8 LogicalSide (net.minecraftforge.fml.LogicalSide)6 PlayerEntity (net.minecraft.entity.player.PlayerEntity)5 OnlyIn (net.minecraftforge.api.distmarker.OnlyIn)5 PerkTree (hellfirepvp.astralsorcery.common.perk.PerkTree)4 Nullable (javax.annotation.Nullable)4 ItemStack (net.minecraft.item.ItemStack)4 ListNBT (net.minecraft.nbt.ListNBT)4 Dist (net.minecraftforge.api.distmarker.Dist)4 AstralSorcery (hellfirepvp.astralsorcery.AstralSorcery)3 PlayerProgress (hellfirepvp.astralsorcery.common.data.research.PlayerProgress)3 ByteBufUtils (hellfirepvp.astralsorcery.common.util.data.ByteBufUtils)3 java.util (java.util)3 Predicate (java.util.function.Predicate)3 Collectors (java.util.stream.Collectors)3 StringNBT (net.minecraft.nbt.StringNBT)3 Constants (net.minecraftforge.common.util.Constants)3 PlayerPerkData (hellfirepvp.astralsorcery.common.data.research.PlayerPerkData)2