Skip to content

Commit

Permalink
[MCPets 4.1.3]
Browse files Browse the repository at this point in the history
* Fixing perms access on category opening command
* Categories now follow the rules fixed by InventorySize in the config.yml
* Fixing skills not working for Despawn and Taming
* Clarification of the EXP gain when you already own the evolution, or when you reach max level
* Fixing a related bug to level up and EXP
* Adding a max_health placeholder in PAPI
  • Loading branch information
AlexandreChaussard committed Mar 31, 2024
1 parent d1d5b3b commit 8b881f3
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public ArgumentCategory(CommandSender sender, String[] args)
}

@Override
public boolean additionalConditions()
{
return sender.hasPermission(PPermission.ADMIN.getPermission());
public boolean additionalConditions() {
return true;
}

@Override
public void commandEffect() {
if(args.length == 2 && sender instanceof Player)
if(args.length == 2 && sender instanceof Player && sender.hasPermission(PPermission.USE.getPermission()))
{
String categoryId = args[1];

Expand All @@ -37,7 +36,7 @@ public void commandEffect() {

category.openInventory((Player)sender, 0);
}
else if(args.length == 3)
else if(args.length == 3 && sender.hasPermission(PPermission.ADMIN.getPermission()))
{
String categoryId = args[1];

Expand Down
23 changes: 20 additions & 3 deletions src/main/java/fr/nocsy/mcpets/data/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.nocsy.mcpets.data.config.CategoryConfig;
import fr.nocsy.mcpets.data.config.FormatArg;
import fr.nocsy.mcpets.data.config.GlobalConfig;
import fr.nocsy.mcpets.data.config.Language;
import lombok.Getter;
import lombok.Setter;
Expand Down Expand Up @@ -63,15 +64,20 @@ public boolean openInventory(Player p, int page)

p.closeInventory();

int invSize = pets.size() - page*53 + 1; //Adding 1 for the page manager
int invSize = GlobalConfig.getInstance().getAdaptiveInventory();
// If we're using the adaptive inventory, we need to calculate the size of the inventory
if(invSize <= 0)
{
invSize = pets.size() - page * 53 + 1; //Adding 1 for the page manager
}
invSize = Math.min(54, invSize);
while(invSize <= 0 || invSize%9 != 0)
{
invSize++;
}

ArrayList<Pet> showedPets = new ArrayList<>();
for(int i = page*53; i < pets.size(); i++)
for(int i = page*(invSize-1); i < pets.size(); i++)
{
if(showedPets.size() >= invSize-1)
break;
Expand Down Expand Up @@ -113,9 +119,20 @@ public void countMaxPages()
{
this.maxPages = 1;
int count = pets.size();
int invSize = GlobalConfig.getInstance().getAdaptiveInventory();
if(invSize > 0)
{
while (invSize <= 0 || invSize % 9 != 0)
invSize++;
invSize--;
}
else
{
invSize = 53;
}
while(count > 0)
{
if(count%53 == 0)
if(count%invSize == 0)
maxPages++;
count--;
}
Expand Down
65 changes: 41 additions & 24 deletions src/main/java/fr/nocsy/mcpets/data/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ public class Pet {

@Getter
@Setter
private Skill despawnSkill;
private String despawnSkill;

@Getter
@Setter
private Skill tamingProgressSkill;
private String tamingProgressSkill;

@Getter
@Setter
private Skill tamingOverSkill;
private String tamingOverSkill;

@Getter
@Setter
Expand Down Expand Up @@ -489,17 +489,19 @@ public void run() {
petStats.setHealth(petStats.getCurrentLevel().getMaxHealth());
}
}.runTaskLater(MCPets.getInstance(), 2L);
if (tamingOverSkill != null) {
Skill tamingOverSkillMM = Utils.getSkill(tamingOverSkill);
if (tamingOverSkillMM != null) {
try {
tamingOverSkill.execute(new SkillMetadataImpl(SkillTriggers.CUSTOM, activeMob, activeMob.getEntity()));
tamingOverSkillMM.execute(new SkillMetadataImpl(SkillTriggers.CUSTOM, activeMob, activeMob.getEntity()));
} catch (Exception ignored) {}
}
}
else
{
if (tamingProgressSkill != null) {
Skill tamingProgressSkillMM = Utils.getSkill(tamingProgressSkill);
if (tamingProgressSkillMM != null) {
try {
tamingProgressSkill.execute(new SkillMetadataImpl(SkillTriggers.CUSTOM, activeMob, activeMob.getEntity()));
tamingProgressSkillMM.execute(new SkillMetadataImpl(SkillTriggers.CUSTOM, activeMob, activeMob.getEntity()));
} catch (Exception ignored) {}
}
}
Expand Down Expand Up @@ -971,10 +973,11 @@ public boolean despawn(PetDespawnReason reason) {
if(reason != PetDespawnReason.DEATH)
{
// Do we have a despawn skill to trigger or a skin swap?
if (despawnSkill != null
Skill despawnSkillMM = Utils.getSkill(despawnSkill);
if (despawnSkillMM != null
&& reason != PetDespawnReason.SKIN) {
try {
despawnSkill.execute(new SkillMetadataImpl(SkillTriggers.CUSTOM, activeMob, activeMob.getEntity()));
despawnSkillMM.execute(new SkillMetadataImpl(SkillTriggers.CUSTOM, activeMob, activeMob.getEntity()));
} catch (Exception ex) {
if (activeMob.getEntity() != null && activeMob.getEntity().getBukkitEntity() != null)
{
Expand Down Expand Up @@ -1147,6 +1150,8 @@ public Pet copy() {
pet.setSpawnRange(spawnRange);
pet.setComingBackRange(comingBackRange);
pet.setDespawnSkill(despawnSkill);
pet.setTamingProgressSkill(tamingProgressSkill);
pet.setTamingOverSkill(tamingOverSkill);
pet.setMountable(mountable);
pet.setMountPermission(mountPermission);
pet.setDespawnOnDismount(despawnOnDismount);
Expand Down Expand Up @@ -1464,21 +1469,33 @@ public ItemStack applyStats(ItemStack item) {
StringBuilder progressBar = new StringBuilder();
PetLevel nextLevel = petStats.getNextLevel();
if (nextLevel != null) {
// Size of the progress bar in the hovering
int progressBarSize = GlobalConfig.instance.getExperienceBarSize();

double experienceRatio = (petStats.getExperience() - petStats.getCurrentLevel().getExpThreshold()) / (nextLevel.getExpThreshold() - petStats.getCurrentLevel().getExpThreshold());
int indexProgress = Math.min(progressBarSize, (int) (experienceRatio * progressBarSize + 0.5));

for (int i = 0; i < progressBarSize; i++) {
if (i < indexProgress)
progressBar.append(GlobalConfig.getInstance().getExperienceColorDone() +
GlobalConfig.getInstance().getExperienceSymbol() +
GlobalConfig.getInstance().getExperienceColorLeft());
else
progressBar.append(GlobalConfig.getInstance().getExperienceColorLeft() +
GlobalConfig.getInstance().getExperienceSymbol() +
GlobalConfig.getInstance().getExperienceColorLeft());
if(nextLevel.equals(petStats.getCurrentLevel()))
{
progressBar.append(Language.PET_STATS_MAX_LEVEL.getMessage());
}
else
{
// Size of the progress bar in the hovering
int progressBarSize = GlobalConfig.instance.getExperienceBarSize();

double experienceRatio = (petStats.getExperience() - petStats.getCurrentLevel().getExpThreshold()) / (nextLevel.getExpThreshold() - petStats.getCurrentLevel().getExpThreshold());
int indexProgress = Math.min(progressBarSize, (int) (experienceRatio * progressBarSize + 0.5));

for (int i = 0; i < progressBarSize; i++) {
if (i < indexProgress)
progressBar.append(GlobalConfig.getInstance().getExperienceColorDone() +
GlobalConfig.getInstance().getExperienceSymbol() +
GlobalConfig.getInstance().getExperienceColorLeft());
else
progressBar.append(GlobalConfig.getInstance().getExperienceColorLeft() +
GlobalConfig.getInstance().getExperienceSymbol() +
GlobalConfig.getInstance().getExperienceColorLeft());
}
}
if(nextLevel.getEvolutionId() != null &&
!nextLevel.canEvolve(owner, Pet.getFromId(nextLevel.getEvolutionId())))
{
progressBar.append('\n').append(Language.PET_STATS_EVOLUTION_ALREADY_OWNED.getMessage());
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/fr/nocsy/mcpets/data/config/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public enum Language {
"\n§7Experience: §a%experience%/%threshold% xp" +
"\n%progressbar%"),

PET_STATS_EVOLUTION_ALREADY_OWNED("§cEvolution already owned."),
PET_STATS_MAX_LEVEL("§7Maximum level reached."),
DEBUGGER_JOINING("§aDebugger is enabled. You are now listening to it."),
DEBUGGER_LEAVE("§aDebugger is §7disabled§a. You will not be listening to it anymore.");

Expand Down
33 changes: 3 additions & 30 deletions src/main/java/fr/nocsy/mcpets/data/config/PetConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,36 +195,9 @@ public void reload() {
pet.setSignals(signals);
pet.setEnableSignalStickFromMenu(enableSignalStickFromMenu);

if (despawnSkillName != null) {
new BukkitRunnable() {
@Override
public void run() {
Optional<Skill> optionalSkill = MCPets.getMythicMobs().getSkillManager().getSkill(despawnSkillName);
optionalSkill.ifPresent(pet::setDespawnSkill);
if (pet.getDespawnSkill() == null) {
MCPets.getLog().warning(MCPets.getLogName() + "Impossible to link the despawn skill \"" + despawnSkillName + "\" to the pet \"" + pet.getId() + "\", because this skill doesn't exist.");
}
}
}.runTaskLater(MCPets.getInstance(), 5L);
}
if (tamingSkillName != null) {
new BukkitRunnable() {
@Override
public void run() {
Optional<Skill> optionalSkill = MCPets.getMythicMobs().getSkillManager().getSkill(tamingSkillName);
optionalSkill.ifPresent(pet::setTamingProgressSkill);
}
}.runTaskLater(MCPets.getInstance(), 5L);
}
if (tamingOverSkillName != null) {
new BukkitRunnable() {
@Override
public void run() {
Optional<Skill> optionalSkill = MCPets.getMythicMobs().getSkillManager().getSkill(tamingOverSkillName);
optionalSkill.ifPresent(pet::setTamingOverSkill);
}
}.runTaskLater(MCPets.getInstance(), 5L);
}
pet.setDespawnSkill(despawnSkillName);
pet.setTamingProgressSkill(tamingSkillName);
pet.setTamingOverSkill(tamingOverSkillName);

ItemStack icon = legacyItemRead(pet.getIcon(), true, pet.toString(), "§cIcon (not set)", "Icon");
pet.setIcon(icon);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/fr/nocsy/mcpets/data/editor/EditorItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ public enum EditorItems {
PET_EDITOR_SPAWN_RANGE(PET_EDITOR_SPAWN_RANGE(), "SpawnRange", null, EditorExpectationType.FLOAT, null, true),
PET_EDITOR_COMING_BACK_RANGE(PET_EDITOR_COMING_BACK_RANGE(), "ComingBackRange", null, EditorExpectationType.FLOAT, null, true),
PET_EDITOR_INVENTORY_SIZE(PET_EDITOR_INVENTORY_SIZE(), "InventorySize", null, EditorExpectationType.INT, null, true),
PET_EDITOR_TAMING_PROGRESS_SKILL(PET_EDITOR_TAMING_PROGRESS_SKILL(), "Taming.TamingProgressSkill", null, EditorExpectationType.INT, null, true),
PET_EDITOR_TAMING_FINISHED_SKILL(PET_EDITOR_TAMING_FINISHED_SKILL(), "Taming.TamingFinishedSkill", null, EditorExpectationType.INT, null, true),
PET_EDITOR_TAMING_PROGRESS_SKILL(PET_EDITOR_TAMING_PROGRESS_SKILL(), "Taming.TamingProgressSkill", null, EditorExpectationType.STRING, null, true),
PET_EDITOR_TAMING_FINISHED_SKILL(PET_EDITOR_TAMING_FINISHED_SKILL(), "Taming.TamingFinishedSkill", null, EditorExpectationType.STRING, null, true),
PET_EDITOR_SIGNALS(PET_EDITOR_SIGNALS(), "Signals.Values", null, EditorExpectationType.STRING_LIST, null, true),
PET_EDITOR_SIGNAL_STICK(PET_EDITOR_SIGNAL_STICK(), "Signals.Item.Raw", null, EditorExpectationType.ITEM, null, true),
PET_EDITOR_GET_SIGNAL_STICK_FROM_MENU(PET_EDITOR_GET_SIGNAL_STICK_FROM_MENU(), "Signals.Item.GetFromMenu", null, EditorExpectationType.BOOLEAN, null, true),
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/fr/nocsy/mcpets/data/livingpets/PetLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ public void playSkill(UUID owner)
*/
public boolean canEvolve(UUID player, Pet evolution)
{
if(player == null || evolution == null)
if(player == null)
return false;
// If there are no evolutions, then it technically can evolve, towards nothing
if (evolution == null)
return true;
// If the owner already has the evolution, then we say that the pet can not evolve
// Else it can evolve
return !Utils.hasPermission(player, evolution.getPermission());
}

Expand Down
17 changes: 15 additions & 2 deletions src/main/java/fr/nocsy/mcpets/data/livingpets/PetStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@ public boolean addExperience(double value)
boolean levelUp = false;
while(!nextLevel.equals(currentLevel) && nextLevel.getExpThreshold() <= experience)
{
if(nextLevel.getEvolutionId() != null &&
!nextLevel.canEvolve(pet.getOwner(), Pet.getFromId(nextLevel.getEvolutionId())))
{
Debugger.send("Pet §6" + pet.getId() + "§7 can not evolve into §a" + nextLevel.getEvolutionId() + "§7 because the player already owns the evolution.");
if(experience == nextLevel.getExpThreshold()-1 + event.getExperience()) {
experience = nextLevel.getExpThreshold() - 1;
return false;
}
else
{
experience = nextLevel.getExpThreshold()-1;
break;
}
}
Debugger.send("§aPet §7" + pet.getId() + "§a is leveling up to §6" + nextLevel.getLevelName());
// note that's there's been a levelup
levelUp = true;
Expand Down Expand Up @@ -364,8 +378,7 @@ public PetLevel getNextLevel()
return null;

return pet.getPetLevels().stream()
.filter(petLevel -> petLevel.getExpThreshold() > currentLevel.getExpThreshold() &&
petLevel.canEvolve(pet.getOwner(), Pet.getFromId(petLevel.getEvolutionId())))
.filter(petLevel -> petLevel.getExpThreshold() > currentLevel.getExpThreshold())
.findFirst().orElse(currentLevel);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,14 +119,22 @@ public static void registerPlaceholders()
return pet.getPetStats().getCurrentLevel().getLevelName();
}));

// Level name placeholder
register("pet.hp", Placeholder.meta((meta,arg) -> {
AbstractEntity entity = meta.getCaster().getEntity();
Pet pet = Pet.getFromEntity(entity.getBukkitEntity());
if(pet == null || pet.getPetStats() == null)
return Double.toString(entity.getHealth());
return Integer.toString((int)entity.getHealth());
else
return Integer.toString((int)pet.getPetStats().getCurrentHealth());
}));

register("pet.max.hp", Placeholder.meta((meta,arg) -> {
AbstractEntity entity = meta.getCaster().getEntity();
Pet pet = Pet.getFromEntity(entity.getBukkitEntity());
if(pet == null || pet.getPetStats() == null)
return Integer.toString((int)entity.getMaxHealth());
else
return Double.toString(pet.getPetStats().getCurrentHealth());
return Integer.toString((int)pet.getPetStats().getCurrentLevel().getMaxHealth());
}));
}

Expand Down
12 changes: 8 additions & 4 deletions src/main/java/fr/nocsy/mcpets/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.google.gson.JsonParser;
import fr.nocsy.mcpets.MCPets;
import fr.nocsy.mcpets.data.config.BlacklistConfig;
import fr.nocsy.mcpets.utils.debug.Debugger;
import io.lumine.mythic.api.skills.Skill;
import me.clip.placeholderapi.PlaceholderAPI;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
Expand All @@ -22,10 +24,7 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -314,4 +313,9 @@ public static String applyPlaceholders(UUID uuid, String msg)
return PlaceholderAPI.setPlaceholders(p, msg);
}

public static Skill getSkill(String skillName) {
Optional<Skill> optionalSkill = MCPets.getMythicMobs().getSkillManager().getSkill(skillName);
return optionalSkill.orElse(null);
}

}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: MCPets
main: fr.nocsy.mcpets.MCPets
version: 4.1.2
version: 4.1.3
description: MCModels affiliate plugin. Join the workshop ! https://discord.gg/p7QTm2gUyf
depend:
- MythicMobs
Expand Down

0 comments on commit 8b881f3

Please sign in to comment.