Added two new placeholders:

%mmocore_profession_experience_<profession>%
%mmocore_profession_next_level_<profession>%
(more information on the wiki)

API Changes:
CustomBlockMineEvent now has a new method to check if the block was actually broken or not.
This commit is contained in:
ASangarin 2020-11-29 00:58:56 +01:00
parent c0bdf1ee12
commit 8812e63b3e
4 changed files with 49 additions and 30 deletions

View File

@ -22,9 +22,10 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
private final List<ItemStack> drops; private final List<ItemStack> drops;
private final ExperienceInfo experience; private final ExperienceInfo experience;
private boolean canBreak;
private boolean cancelled = false; private boolean cancelled = false;
public CustomBlockMineEvent(PlayerData player, Block block, BlockInfo info) { public CustomBlockMineEvent(PlayerData player, Block block, BlockInfo info, boolean canBreak) {
super(player); super(player);
this.block = block; this.block = block;
@ -33,6 +34,7 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
&& info.getDropTable().areConditionsMet(new ConditionInstance(player.getPlayer()))) && info.getDropTable().areConditionsMet(new ConditionInstance(player.getPlayer())))
? info.collectDrops(new LootBuilder(player, 0)) : new ArrayList<>(); ? info.collectDrops(new LootBuilder(player, 0)) : new ArrayList<>();
this.experience = info.hasExperience() ? info.getExperience().newInfo() : null; this.experience = info.hasExperience() ? info.getExperience().newInfo() : null;
this.canBreak = canBreak;
} }
public Block getBlock() { public Block getBlock() {
@ -55,6 +57,14 @@ public class CustomBlockMineEvent extends PlayerDataEvent implements Cancellable
return experience; return experience;
} }
public boolean canBreak() {
return canBreak;
}
public void setCanBreak(boolean value) {
canBreak = value;
}
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
return cancelled; return cancelled;

View File

@ -76,7 +76,7 @@ public class PlayerProfessions {
} }
public int getLevel(String profession) { public int getLevel(String profession) {
return Math.max(1, level.containsKey(profession) ? level.get(profession) : 1); return Math.max(1, level.getOrDefault(profession, 1));
} }
public int getLevel(Profession profession) { public int getLevel(Profession profession) {
@ -84,7 +84,7 @@ public class PlayerProfessions {
} }
public int getExperience(String id) { public int getExperience(String id) {
return exp.containsKey(id) ? exp.get(id) : 0; return exp.getOrDefault(id, 0);
} }
public int getExperience(Profession profession) { public int getExperience(Profession profession) {
@ -95,6 +95,11 @@ public class PlayerProfessions {
return profession.getExpCurve().getExperience(getLevel(profession) + 1); return profession.getExpCurve().getExperience(getLevel(profession) + 1);
} }
public int getLevelUpExperience(String id) {
if(!MMOCore.plugin.professionManager.has(id)) return 0;
return MMOCore.plugin.professionManager.get(id).getExpCurve().getExperience(getLevel(id) + 1);
}
public void setLevel(Profession profession, int value) { public void setLevel(Profession profession, int value) {
level.put(profession.getId(), value); level.put(profession.getId(), value);
} }

View File

@ -1,12 +1,5 @@
package net.Indyuce.mmocore.comp.placeholder; package net.Indyuce.mmocore.comp.placeholder;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import me.clip.placeholderapi.expansion.PlaceholderExpansion; import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.experience.PlayerProfessions; import net.Indyuce.mmocore.api.experience.PlayerProfessions;
@ -15,6 +8,12 @@ import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.stats.StatType; import net.Indyuce.mmocore.api.player.stats.StatType;
import net.Indyuce.mmocore.api.quest.PlayerQuests; import net.Indyuce.mmocore.api.quest.PlayerQuests;
import net.mmogroup.mmolib.api.util.AltChar; import net.mmogroup.mmolib.api.util.AltChar;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.attribute.Attribute;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class RPGPlaceholders extends PlaceholderExpansion { public class RPGPlaceholders extends PlaceholderExpansion {
@Override @Override
@ -74,9 +73,7 @@ public class RPGPlaceholders extends PlaceholderExpansion {
double ratio = 20 * player.getPlayer().getHealth() double ratio = 20 * player.getPlayer().getHealth()
/ player.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); / player.getPlayer().getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue();
for (double j = 1; j < 20; j++) for (double j = 1; j < 20; j++)
format.append((ratio >= j ? ChatColor.RED format.append(ratio >= j ? ChatColor.RED : ratio >= j - .5 ? ChatColor.DARK_RED : ChatColor.DARK_GRAY).append(AltChar.listSquare);
: ratio >= j - .5 ? ChatColor.DARK_RED : ChatColor.DARK_GRAY)
+ AltChar.listSquare);
return format.toString(); return format.toString();
} }
@ -98,6 +95,14 @@ public class RPGPlaceholders extends PlaceholderExpansion {
: MMOCore.plugin.configManager.noSkillBoundPlaceholder; : MMOCore.plugin.configManager.noSkillBoundPlaceholder;
} }
else if (identifier.startsWith("profession_experience_"))
return "" + PlayerData.get(player).getCollectionSkills()
.getExperience(identifier.substring(22).replace(" ", "-").replace("_", "-").toLowerCase());
else if (identifier.startsWith("profession_next_level_"))
return "" + PlayerData.get(player).getCollectionSkills()
.getLevelUpExperience(identifier.substring(21).replace(" ", "-").replace("_", "-").toLowerCase());
else if (identifier.startsWith("profession_")) else if (identifier.startsWith("profession_"))
return "" + PlayerData.get(player).getCollectionSkills() return "" + PlayerData.get(player).getCollectionSkills()
.getLevel(identifier.substring(11).replace(" ", "-").replace("_", "-").toLowerCase()); .getLevel(identifier.substring(11).replace(" ", "-").replace("_", "-").toLowerCase());
@ -148,14 +153,12 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return MMOCore.plugin.configManager.decimal.format(PlayerData.get(player).getStamina()); return MMOCore.plugin.configManager.decimal.format(PlayerData.get(player).getStamina());
else if (identifier.equals("stamina_bar")) { else if (identifier.equals("stamina_bar")) {
String format = ""; StringBuilder format = new StringBuilder();
PlayerData data = PlayerData.get(player); PlayerData data = PlayerData.get(player);
double ratio = 20 * data.getStamina() / data.getStats().getStat(StatType.MAX_STAMINA); double ratio = 20 * data.getStamina() / data.getStats().getStat(StatType.MAX_STAMINA);
for (double j = 1; j < 20; j++) for (double j = 1; j < 20; j++)
format += (ratio >= j ? MMOCore.plugin.configManager.staminaFull format.append(ratio >= j ? MMOCore.plugin.configManager.staminaFull : ratio >= j - .5 ? MMOCore.plugin.configManager.staminaHalf : MMOCore.plugin.configManager.staminaEmpty).append(AltChar.listSquare);
: ratio >= j - .5 ? MMOCore.plugin.configManager.staminaHalf : MMOCore.plugin.configManager.staminaEmpty) return format.toString();
+ AltChar.listSquare;
return format;
} }
else if (identifier.startsWith("stat_")) { else if (identifier.startsWith("stat_")) {
@ -167,12 +170,12 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return MMOCore.plugin.configManager.decimal.format(PlayerData.get(player).getStellium()); return MMOCore.plugin.configManager.decimal.format(PlayerData.get(player).getStellium());
else if (identifier.equals("stellium_bar")) { else if (identifier.equals("stellium_bar")) {
String format = ""; StringBuilder format = new StringBuilder();
PlayerData data = PlayerData.get(player); PlayerData data = PlayerData.get(player);
double ratio = 20 * data.getStellium() / data.getStats().getStat(StatType.MAX_STELLIUM); double ratio = 20 * data.getStellium() / data.getStats().getStat(StatType.MAX_STELLIUM);
for (double j = 1; j < 20; j++) for (double j = 1; j < 20; j++)
format += (ratio >= j ? ChatColor.BLUE : ratio >= j - .5 ? ChatColor.AQUA : ChatColor.WHITE) + AltChar.listSquare; format.append(ratio >= j ? ChatColor.BLUE : ratio >= j - .5 ? ChatColor.AQUA : ChatColor.WHITE).append(AltChar.listSquare);
return format; return format.toString();
} }
else if (identifier.equals("quest")) { else if (identifier.equals("quest")) {

View File

@ -76,20 +76,21 @@ public class BlockListener implements Listener {
return; return;
} }
boolean canBreak = true;
ItemStack item = player.getInventory().getItemInMainHand();
if (!MMOCore.plugin.restrictionManager.checkPermissions(item, info.getBlock())) {
MMOCore.plugin.configManager.getSimpleMessage("cannot-break").send(player);
canBreak = false;
}
/* /*
* Calls the event and listen for cancel & for drops changes... also * Calls the event and listen for cancel & for drops changes... also
* allows to apply tool durability & enchants to drops, etc. * allows to apply tool durability & enchants to drops, etc.
*/ */
CustomBlockMineEvent called = new CustomBlockMineEvent(PlayerData.get(player), block, info); CustomBlockMineEvent called = new CustomBlockMineEvent(PlayerData.get(player), block, info, canBreak);
Bukkit.getPluginManager().callEvent(called); Bukkit.getPluginManager().callEvent(called);
if (called.isCancelled()) {
event.setCancelled(true);
return;
}
ItemStack item = player.getInventory().getItemInMainHand(); if(called.isCancelled() || !called.canBreak()) {
if (!MMOCore.plugin.restrictionManager.checkPermissions(item, info.getBlock())) {
MMOCore.plugin.configManager.getSimpleMessage("cannot-break").send(player);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -141,7 +142,7 @@ public class BlockListener implements Listener {
if (!movedBlock.hasMetadata("player_placed")) if (!movedBlock.hasMetadata("player_placed"))
return; return;
BlockFace direction = event.getDirection(); BlockFace direction = event.getDirection();
movedBlock = movedBlock.getRelative(direction, 2); //movedBlock = movedBlock.getRelative(direction, 2);
for (Block b : event.getBlocks()) for (Block b : event.getBlocks())
if (b.hasMetadata("player_placed")) { if (b.hasMetadata("player_placed")) {