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));
}
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));
}
}
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) {
}
};
}
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;
}
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;
}
Aggregations