use of de.Keyle.MyPet.api.skill.Upgrade in project MyPet by xXKeyleXx.
the class SkillTreeLoaderJSON method loadSkilltree.
public static void loadSkilltree(JsonObject skilltreeObject) {
if (!containsKey(skilltreeObject, "ID")) {
return;
}
Skilltree skilltree;
String skilltreeID = get(skilltreeObject, "ID").getAsString();
if (MyPetApi.getSkilltreeManager().hasSkilltree(skilltreeID)) {
return;
}
skilltree = new Skilltree(skilltreeID);
tryToLoad("Name", () -> {
if (containsKey(skilltreeObject, "Name")) {
skilltree.setDisplayName(get(skilltreeObject, "Name").getAsString());
}
});
tryToLoad("Permission", () -> {
if (containsKey(skilltreeObject, "Permission")) {
String permission = get(skilltreeObject, "Permission").getAsString();
Settings settings = new Settings("Permission");
settings.load(permission);
skilltree.addRequirementSettings(settings);
// TODO warnung zum aktualisieren
}
});
tryToLoad("Display", () -> {
if (containsKey(skilltreeObject, "Display")) {
skilltree.setDisplayName(get(skilltreeObject, "Display").getAsString());
}
});
tryToLoad("MaxLevel", () -> {
if (containsKey(skilltreeObject, "MaxLevel")) {
skilltree.setMaxLevel((get(skilltreeObject, "MaxLevel").getAsInt()));
}
});
tryToLoad("RequiredLevel", () -> {
if (containsKey(skilltreeObject, "RequiredLevel")) {
skilltree.setRequiredLevel((get(skilltreeObject, "RequiredLevel").getAsInt()));
}
});
tryToLoad("Order", () -> {
if (containsKey(skilltreeObject, "Order")) {
skilltree.setOrder((get(skilltreeObject, "Order").getAsInt()));
}
});
tryToLoad("Weight", () -> {
if (containsKey(skilltreeObject, "Weight")) {
skilltree.setWeight((get(skilltreeObject, "Weight").getAsDouble()));
}
});
tryToLoad("MobTypes", () -> {
if (containsKey(skilltreeObject, "MobTypes")) {
List<MyPetType> availableTypes = Arrays.stream(MyPetType.values()).filter(MyPetType::checkMinecraftVersion).collect(Collectors.toList());
JsonArray mobTypeArray = get(skilltreeObject, "MobTypes").getAsJsonArray();
Set<MyPetType> mobTypes = new HashSet<>();
if (mobTypeArray.size() == 0) {
mobTypes.addAll(availableTypes);
} else {
boolean allNegative = true;
for (JsonElement o : mobTypeArray) {
String type = o.getAsString();
if (!type.startsWith("-")) {
allNegative = false;
break;
}
}
if (allNegative) {
mobTypes.addAll(availableTypes);
}
mobTypeArray.forEach(jsonElement -> {
String type = jsonElement.getAsString();
if (type.equals("*")) {
mobTypes.addAll(availableTypes);
} else {
boolean negative = false;
if (type.startsWith("-")) {
type = type.substring(1);
negative = true;
}
MyPetType mobType = MyPetType.byName(type, false);
if (mobType != null && mobType.checkMinecraftVersion()) {
if (negative) {
mobTypes.remove(mobType);
} else {
mobTypes.add(mobType);
}
}
}
});
}
skilltree.setMobTypes(mobTypes);
}
});
tryToLoad("Icon", () -> {
if (containsKey(skilltreeObject, "Icon")) {
JsonObject iconObject = get(skilltreeObject, "Icon").getAsJsonObject();
SkilltreeIcon icon = new SkilltreeIcon();
tryToLoad("Icon.Material", () -> {
if (containsKey(iconObject, "Material")) {
icon.setMaterial(get(iconObject, "Material").getAsString());
}
});
tryToLoad("Icon.Glowing", () -> {
if (containsKey(iconObject, "Glowing")) {
icon.setGlowing(get(iconObject, "Glowing").getAsBoolean());
}
});
skilltree.setIcon(icon);
}
});
tryToLoad("Inheritance", () -> {
if (containsKey(skilltreeObject, "Inheritance")) {
JsonObject inheritanceObject = get(skilltreeObject, "Inheritance").getAsJsonObject();
if (containsKey(inheritanceObject, "Skilltree")) {
skilltree.setInheritedSkilltreeName(get(inheritanceObject, "Skilltree").getAsString());
}
}
});
tryToLoad("Description", () -> {
if (containsKey(skilltreeObject, "Description")) {
JsonArray descriptionArray = get(skilltreeObject, "Description").getAsJsonArray();
descriptionArray.forEach(jsonElement -> skilltree.addDescriptionLine(jsonElement.getAsString()));
}
});
tryToLoad("Notifications", () -> {
if (containsKey(skilltreeObject, "Notifications")) {
JsonObject notificationsObject = get(skilltreeObject, "Notifications").getAsJsonObject();
for (String levelRuleString : notificationsObject.keySet()) {
tryToLoad("Notification." + levelRuleString, () -> {
LevelRule levelRule = loadLevelRule(levelRuleString);
String message = notificationsObject.get(levelRuleString).getAsString();
skilltree.addNotification(levelRule, message);
});
}
}
});
tryToLoad("Requirements", () -> {
if (containsKey(skilltreeObject, "Requirements")) {
JsonArray requirementsArray = get(skilltreeObject, "Requirements").getAsJsonArray();
requirementsArray.forEach(jsonElement -> {
boolean hasParameter = jsonElement.getAsString().contains(":");
String[] data = jsonElement.getAsString().split(":", 2);
Settings settings = new Settings(data[0]);
if (hasParameter) {
tryToLoad("Requirement." + jsonElement.getAsString(), () -> settings.load(data[1]));
}
skilltree.addRequirementSettings(settings);
});
}
});
tryToLoad("Skills", () -> {
if (containsKey(skilltreeObject, "Skills")) {
JsonObject skillsObject = get(skilltreeObject, "Skills").getAsJsonObject();
for (String skillName : skillsObject.keySet()) {
JsonObject skillObject = skillsObject.getAsJsonObject(skillName);
tryToLoad("Skills." + skillName + ".Upgrades", () -> {
if (containsKey(skillObject, "Upgrades")) {
JsonObject upgradesObject = get(skillObject, "Upgrades").getAsJsonObject();
for (String levelRuleString : upgradesObject.keySet()) {
tryToLoad("Skills." + skillName + ".Upgrades." + levelRuleString, () -> {
LevelRule levelRule = loadLevelRule(levelRuleString);
JsonObject upgradeObject = upgradesObject.getAsJsonObject(levelRuleString);
tryToLoad("Skills." + skillName + ".Upgrades." + levelRuleString + ".Upgrade", () -> {
Upgrade upgrade = loadUpgrade(skillName, upgradeObject);
skilltree.addUpgrade(levelRule, upgrade);
});
});
}
}
});
}
}
});
MyPetApi.getSkilltreeManager().registerSkilltree(skilltree);
}
use of de.Keyle.MyPet.api.skill.Upgrade in project MyPet by xXKeyleXx.
the class LevelListener method on.
@EventHandler
public void on(MyPetLevelEvent event) {
if (event instanceof MyPetLevelUpEvent || event instanceof MyPetLevelDownEvent) {
return;
}
MyPet myPet = event.getPet();
int lvl = event.getLevel();
myPet.getSkills().all().forEach(Skill::reset);
Skilltree skilltree = myPet.getSkilltree();
if (skilltree != null) {
for (int i = 1; i <= lvl; i++) {
List<Upgrade> upgrades = skilltree.getUpgrades(i);
for (Upgrade upgrade : upgrades) {
SkillName sn = Util.getClassAnnotation(upgrade.getClass(), SkillName.class);
if (sn != null) {
Skill skill = myPet.getSkills().get(sn.value());
if (skill != null) {
upgrade.apply(skill);
}
}
}
}
}
}
use of de.Keyle.MyPet.api.skill.Upgrade in project MyPet by xXKeyleXx.
the class LevelListener method on.
@EventHandler
public void on(MyPetLevelDownEvent event) {
MyPet myPet = event.getPet();
int lvl = event.getLevel();
int fromLvl = event.fromLevel();
if (!event.isQuiet()) {
myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.LevelDown", event.getOwner()), myPet.getPetName(), event.getLevel()));
}
Skilltree skilltree = myPet.getSkilltree();
if (skilltree != null) {
for (int i = fromLvl; i > lvl; i--) {
List<Upgrade> upgrades = skilltree.getUpgrades(i);
for (Upgrade upgrade : upgrades) {
SkillName sn = Util.getClassAnnotation(upgrade.getClass(), SkillName.class);
if (sn != null) {
Skill skill = myPet.getSkills().get(sn.value());
if (skill != null) {
upgrade.invert(skill);
}
}
}
}
}
if (myPet.getStatus() == MyPet.PetState.Here) {
myPet.getEntity().ifPresent(entity -> {
entity.getHandle().updateNameTag();
if (!event.isQuiet()) {
myPet.setHealth(myPet.getMaxHealth());
myPet.setSaturation(100);
new FixedCircleAnimation(1, entity.getEyeHeight() + 0.5, 10, new EntityLocationHolder(entity)) {
@Override
protected void playParticleEffect(Location location) {
MyPetApi.getPlatformHelper().playParticleEffect(location, ParticleCompat.BLOCK_CRACK.get(), 0, 0, 0, 0, 1, 32, ParticleCompat.REDSTONE_BLOCK_DATA);
}
}.once();
entity.getWorld().playSound(entity.getLocation(), Sound.valueOf(SoundCompat.LEVEL_DOWN.get()), 1F, 0.7F);
}
});
}
}
use of de.Keyle.MyPet.api.skill.Upgrade in project MyPet by xXKeyleXx.
the class Skilltree method getUpgrades.
protected List<Upgrade> getUpgrades(int level, Set<String> computedSkilltrees) {
List<Upgrade> upgrades = new ArrayList<>();
computedSkilltrees.add(this.skilltreeName);
if (inheritedSkilltreeName != null && !inheritedSkilltreeName.isEmpty() && !computedSkilltrees.contains(inheritedSkilltreeName)) {
if (MyPetApi.getSkilltreeManager().hasSkilltree(inheritedSkilltreeName)) {
upgrades.addAll(MyPetApi.getSkilltreeManager().getSkilltree(inheritedSkilltreeName).getUpgrades(level, computedSkilltrees));
}
}
List<LevelRule> rules = new ArrayList<>(this.upgrades.keySet());
rules.sort(Comparator.comparingInt(LevelRule::getPriority));
for (LevelRule rule : rules) {
if (rule.check(level)) {
upgrades.add(this.upgrades.get(rule));
}
}
return upgrades;
}
use of de.Keyle.MyPet.api.skill.Upgrade in project MyPet by xXKeyleXx.
the class LevelListener method on.
@EventHandler
public void on(MyPetLevelUpEvent event) {
MyPet myPet = event.getPet();
int lvl = event.getLevel();
int fromLvl = event.fromLevel();
if (!event.isQuiet()) {
int maxlevel = myPet.getSkilltree() != null ? myPet.getSkilltree().getMaxLevel() : 0;
if (maxlevel != 0 && lvl >= maxlevel) {
myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.ReachedMaxLevel", event.getOwner()), myPet.getPetName(), maxlevel));
} else {
myPet.getOwner().sendMessage(Util.formatText(Translation.getString("Message.LevelSystem.LevelUp", event.getOwner()), myPet.getPetName(), event.getLevel()));
}
}
Skilltree skilltree = myPet.getSkilltree();
if (skilltree != null) {
for (int i = fromLvl + 1; i <= lvl; i++) {
if (!event.isQuiet()) {
List<String> notifications = skilltree.getNotifications(i);
for (String notification : notifications) {
notification = notification.replace("{{owner}}", myPet.getOwner().getName()).replace("{{level}}", "" + lvl).replace("{{pet}}", myPet.getPetName());
notification = Colorizer.setColors(notification);
String[] lines = notification.split("(<br>|\\\\n|\n|<br\\s?/>)");
for (String line : lines) {
myPet.getOwner().sendMessage(line);
}
}
}
Set<Skill> affectedSkills = new HashSet<>();
List<Upgrade> upgrades = skilltree.getUpgrades(i);
for (Upgrade upgrade : upgrades) {
SkillName sn = Util.getClassAnnotation(upgrade.getClass(), SkillName.class);
if (sn != null) {
Skill skill = myPet.getSkills().get(sn.value());
if (skill != null) {
upgrade.apply(skill);
affectedSkills.add(skill);
}
}
}
if (!event.isQuiet()) {
for (Skill skill : affectedSkills) {
String[] messages = skill.getUpgradeMessage();
if (messages != null && messages.length > 0) {
for (String message : messages) {
myPet.getOwner().sendMessage(" " + message);
}
}
}
}
}
}
if (myPet.getStatus() == MyPet.PetState.Here) {
myPet.getEntity().ifPresent(entity -> {
entity.getHandle().updateNameTag();
if (!event.isQuiet()) {
myPet.setHealth(myPet.getMaxHealth());
myPet.setSaturation(100);
new SpiralAnimation(1, entity.getEyeHeight() + 0.5, new EntityLocationHolder(entity)) {
@Override
protected void playParticleEffect(Location location) {
MyPetApi.getPlatformHelper().playParticleEffect(location, ParticleCompat.CRIT_MAGIC.get(), 0, 0, 0, 0, 1, 32);
}
}.loop(2);
entity.getWorld().playSound(entity.getLocation(), Sound.valueOf(SoundCompat.LEVEL_UP.get()), 1F, 0.7F);
}
});
}
}
Aggregations