9
0
mirror of https://github.com/Auxilor/EcoSkills.git synced 2026-01-03 14:22:17 +00:00

Even more backend

This commit is contained in:
Auxilor
2021-08-20 15:51:24 +01:00
parent 2510032a65
commit c0f6a6fa83
11 changed files with 339 additions and 33 deletions

View File

@@ -5,6 +5,10 @@ import com.willfp.eco.core.command.impl.PluginCommand;
import com.willfp.ecoskills.commands.CommandEcoskills;
import com.willfp.ecoskills.effects.Effect;
import com.willfp.ecoskills.effects.Effects;
import com.willfp.ecoskills.skills.SkillDisplayListener;
import com.willfp.ecoskills.skills.Skill;
import com.willfp.ecoskills.skills.SkillLevellingListener;
import com.willfp.ecoskills.skills.Skills;
import com.willfp.ecoskills.stats.Stat;
import com.willfp.ecoskills.stats.Stats;
import org.bukkit.event.Listener;
@@ -36,6 +40,10 @@ public class EcoSkillsPlugin extends EcoPlugin {
this.getEventManager().unregisterListener(stat);
this.getEventManager().registerListener(stat);
}
for (Skill skill : Skills.values()) {
this.getEventManager().unregisterListener(skill);
this.getEventManager().registerListener(skill);
}
}
/**
@@ -50,7 +58,8 @@ public class EcoSkillsPlugin extends EcoPlugin {
@Override
protected List<Listener> loadListeners() {
return Arrays.asList(
new SkillLevellingListener(this),
new SkillDisplayListener(this)
);
}

View File

@@ -2,11 +2,12 @@ package com.willfp.ecoskills.api;
import com.willfp.ecoskills.skills.Skill;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
public class PlayerSkillExpGainEvent extends PlayerEvent {
public class PlayerSkillExpGainEvent extends PlayerEvent implements Cancellable {
/**
* Bukkit parity.
*/
@@ -22,6 +23,11 @@ public class PlayerSkillExpGainEvent extends PlayerEvent {
*/
private double amount;
/**
* If the event is cancelled.
*/
private boolean cancelled = false;
/**
* Create a new PlayerSkillExpGainEvent.
*
@@ -64,6 +70,16 @@ public class PlayerSkillExpGainEvent extends PlayerEvent {
this.amount = amount;
}
@Override
public void setCancelled(final boolean cancel) {
this.cancelled = cancel;
}
@Override
public boolean isCancelled() {
return this.cancelled;
}
/**
* Bukkit parity.
*

View File

@@ -0,0 +1,77 @@
package com.willfp.ecoskills.api;
import com.willfp.ecoskills.skills.Skill;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;
public class PlayerSkillLevelUpEvent extends PlayerEvent {
/**
* Bukkit parity.
*/
private static final HandlerList HANDLERS = new HandlerList();
/**
* The skill.
*/
private final Skill skill;
/**
* The amount.
*/
private final int level;
/**
* Create a new PlayerSkillLevelUpEvent.
*
* @param who The player.
* @param skill The skill.
* @param level The level gained.
*/
public PlayerSkillLevelUpEvent(@NotNull final Player who,
@NotNull final Skill skill,
final int level) {
super(who);
this.skill = skill;
this.level = level;
}
/**
* Get the skill for the event.
*
* @return The skill.
*/
public Skill getSkill() {
return this.skill;
}
/**
* Get the level achieved.
*
* @return The level.
*/
public int getLevel() {
return level;
}
/**
* Bukkit parity.
*
* @return The handler list.
*/
@NotNull
@Override
public HandlerList getHandlers() {
return HANDLERS;
}
/**
* Bukkit parity.
*
* @return The handler list.
*/
public static HandlerList getHandlerList() {
return HANDLERS;
}
}

View File

@@ -2,7 +2,7 @@ package com.willfp.ecoskills.skills;
import com.google.common.collect.ImmutableSet;
import com.willfp.eco.core.config.updating.ConfigUpdater;
import com.willfp.ecoskills.EcoSkillsPlugin;
import com.willfp.ecoskills.skills.skills.SkillMining;
import org.bukkit.NamespacedKey;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
@@ -18,7 +18,8 @@ public class Skills {
*/
private static final Map<String, Skill> REGISTRY = new HashMap<>();
public static final Skill MINING = new Skill(PLUGIN, "mining");
public static final Skill MINING = new SkillMining();
/*
public static final Skill COMBAT = new Skill(PLUGIN, "combat");
public static final Skill ENCHANTING = new Skill(PLUGIN, "enchanting");
public static final Skill FARMING = new Skill(PLUGIN, "farming");
@@ -27,6 +28,7 @@ public class Skills {
public static final Skill ALCHEMY = new Skill(PLUGIN, "alchemy");
public static final Skill ARMORY = new Skill(PLUGIN, "armory");
public static final Skill EXPLORATION = new Skill(PLUGIN, "exploration");
*/
@ApiStatus.Internal
public static void registerNewSkill(@NotNull final Skill skill) {

View File

@@ -14,11 +14,11 @@ fun Player.setSkillLevel(skill: Skill, level: Int) {
this.persistentDataContainer.set(skill.key, PersistentDataType.INTEGER, level)
}
fun Player.getSkillExperience(skill: Skill): Double {
fun Player.getSkillProgress(skill: Skill): Double {
return this.persistentDataContainer.getOrDefault(skill.xpKey, PersistentDataType.DOUBLE, 0.0)
}
fun Player.setSkillExperience(skill: Skill, level: Double) {
fun Player.setSkillProgress(skill: Skill, level: Double) {
this.persistentDataContainer.set(skill.xpKey, PersistentDataType.DOUBLE, level)
}

View File

@@ -1,16 +0,0 @@
package com.willfp.ecoskills.skills
import com.willfp.eco.core.EcoPlugin
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
class ProgressDisplayListener(
private val plugin: EcoPlugin
) : Listener {
@EventHandler(priority = EventPriority.MONITOR)
fun onProgress(event: PlayerSkillExpGainEvent) {
}
}

View File

@@ -1,14 +1,21 @@
package com.willfp.ecoskills.skills
import com.google.gson.annotations.Since
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.core.config.interfaces.Config
import com.willfp.ecoskills.EcoSkillsPlugin
import org.bukkit.NamespacedKey
import org.bukkit.event.Listener
import java.math.BigDecimal
import java.math.MathContext
import java.util.*
import kotlin.math.pow
import kotlin.math.round
import kotlin.math.roundToInt
abstract class Skill(
val id: String
) {
): Listener {
protected val plugin: EcoPlugin = EcoSkillsPlugin.getInstance()
val key: NamespacedKey
@@ -30,4 +37,17 @@ abstract class Skill(
fun update() {
name = plugin.langYml.getString("skills.$id.name")
}
fun getExpForLevel(level: Int): Int {
val level1xp = this.plugin.configYml.getInt("skills.level-1-xp")
val multiplier = this.plugin.configYml.getDouble("skills.xp-multiplier-per-level")
val sigFig = this.plugin.configYml.getInt("skills.sig-fig")
val roundTo = this.plugin.configYml.getInt("skills.round-to")
var xp = level1xp * (level.toDouble().pow(multiplier))
val bigDecimal = BigDecimal(xp)
bigDecimal.round(MathContext(sigFig))
xp = bigDecimal.toDouble()
return ((xp / roundTo).roundToInt() * roundTo)
}
}

View File

@@ -0,0 +1,83 @@
package com.willfp.ecoskills.skills
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.util.NumberUtils
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent
import com.willfp.ecoskills.getSkillLevel
import com.willfp.ecoskills.getSkillProgress
import net.md_5.bungee.api.ChatMessageType
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.Sound
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
class SkillDisplayListener(
private val plugin: EcoPlugin
) : Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
fun onProgress(event: PlayerSkillExpGainEvent) {
val player = event.player
val skill = event.skill
val amount = event.amount
if (this.plugin.configYml.getBool("skills.progress.action-bar.enabled")) {
var string = this.plugin.configYml.getString("skills.progress.action-bar.format")
string = string.replace("%skill%", skill.name)
string = string.replace("%current_xp%", NumberUtils.format(player.getSkillProgress(skill)))
string = string.replace(
"%required_xp%",
NumberUtils.format(
skill.getExpForLevel(player.getSkillLevel(skill) + 1).toDouble()
)
)
string = string.replace("%gained_xp%", NumberUtils.format(amount))
player.spigot().sendMessage(
ChatMessageType.ACTION_BAR,
*TextComponent.fromLegacyText(string)
)
}
}
@EventHandler(priority = EventPriority.MONITOR)
fun onLevelUp(event: PlayerSkillLevelUpEvent) {
val player = event.player
val skill = event.skill
val level = event.level
if (this.plugin.configYml.getBool("skills.level-up.sound.enabled")) {
val sound = Sound.valueOf(this.plugin.configYml.getString("skills.level-up.sound.id").uppercase())
val pitch = this.plugin.configYml.getDouble("skills.level-up.sound.pitch")
player.playSound(
player.location,
sound,
100f,
pitch.toFloat()
)
}
if (this.plugin.configYml.getBool("skills.level-up.message.enabled")) {
val messages = ArrayList<String>()
for (string in this.plugin.configYml.getStrings("skills.level-up.message.message")) {
messages.add(
string.replace("%skill%", skill.name)
.replace("%level%", level.toString())
)
}
val rewardIndex = messages.indexOf("%rewards%")
if (rewardIndex != -1) {
messages.removeAt(rewardIndex)
messages.addAll(rewardIndex, listOf())
}
for (message in messages) {
player.sendMessage(message)
}
}
}
}

View File

@@ -0,0 +1,35 @@
package com.willfp.ecoskills.skills
import com.willfp.eco.core.EcoPlugin
import com.willfp.eco.util.NumberUtils
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
import com.willfp.ecoskills.api.PlayerSkillLevelUpEvent
import com.willfp.ecoskills.getSkillLevel
import com.willfp.ecoskills.getSkillProgress
import com.willfp.ecoskills.setSkillLevel
import com.willfp.ecoskills.setSkillProgress
import net.md_5.bungee.api.ChatMessageType
import net.md_5.bungee.api.chat.TextComponent
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.Listener
class SkillLevellingListener(
private val plugin: EcoPlugin
) : Listener {
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
fun onProgress(event: PlayerSkillExpGainEvent) {
val player = event.player
val skill = event.skill
val amount = event.amount
val level = player.getSkillLevel(skill)
if (player.getSkillProgress(skill) + amount >= skill.getExpForLevel(level + 1)) {
player.setSkillProgress(skill, 0.0)
player.setSkillLevel(skill, level + 1)
val levelUpEvent = PlayerSkillLevelUpEvent(player, skill, level + 1)
Bukkit.getPluginManager().callEvent(levelUpEvent)
}
}
}

View File

@@ -0,0 +1,35 @@
package com.willfp.ecoskills.skills.skills
import com.willfp.ecoskills.api.PlayerSkillExpGainEvent
import com.willfp.ecoskills.getSkillProgress
import com.willfp.ecoskills.skills.Skill
import org.bukkit.Bukkit
import org.bukkit.event.EventHandler
import org.bukkit.event.EventPriority
import org.bukkit.event.block.BlockBreakEvent
class SkillMining : Skill(
"mining"
) {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH)
fun handleLevelling(event: BlockBreakEvent) {
val type = event.block.type
val player = event.player
var xp = player.getSkillProgress(this)
var toGive = 0.0
for (string in this.config.getStrings("xp-rewards", false)) {
if (string.startsWith(type.name.lowercase())) {
toGive = string.split(":")[1].toDouble()
break;
}
}
if (toGive == 0.0) {
return
}
val gainEvent = PlayerSkillExpGainEvent(player, this, toGive)
Bukkit.getPluginManager().callEvent(gainEvent)
}
}

View File

@@ -12,59 +12,104 @@ skills:
xp-multiplier-per-level: 1.26
# Since the formula will give unpleasant ratios, then truncate it to an amount of significant figures.
sig-fig: 2
# Always round to the nearest also
round-to: 10
# Ways to tell the player about skill progress
progress:
action-bar:
# If the action bar should be used
enabled: true
format:
# The actionbar message that should be sent
format: "&f%skill% &8| &9(%current_xp%/%required_xp%) &e+%gained_xp%"
# Ways to tell the player about levelling up
level-up:
message:
enabled: true
message:
- "&f"
- "&3You levelled up &9%skill%&3 to level &e%level%&3!"
- "&f"
- "&3&lREWARDS:"
- "%rewards%"
- "&f"
sound:
# If a sound should be played
enabled: true
# The sound that should be played
id: entity_player_levelup
# Pitch between 0.5 and 2
pitch: 0.8
mining:
gui-item: stone_pickaxe
# The maximum obtainable level
max-level: 50
name: "Mining"
# The xp rewards for each block type
# Specify with type:xp
xp-rewards:
- "stone:1"
- "diorite:1"
- "andesite:1"
- "granite:1"
- "tuff:1"
- "deepslate:1"
- "cobblestone:1"
- "netherrack:0.5"
- "sand:3"
- "gravel:4"
- "nether_quartz_ore:5"
- "end_stone:3"
- "coal_ore:5"
- "deepslate_coal_ore:5"
- "iron_ore:5"
- "deepslate_iron_ore:5"
- "gold_ore:6"
- "deepslate_gold_ore:6"
- "lapis_lazuli_ore:7"
- "deepslate_lapis_lazuli_ore:7"
- "redstone_ore:7"
- "deepslate_redstone_ore:7"
- "emerald_ore:9"
- "deepslate_emerald_ore:9"
- "diamond_ore:10"
- "deepslate_diamond_ore:10"
- "ancient_debris:20"
combat:
gui-item: stone_sword
# The maximum obtainable level
max-level: 50
name: "Combat"
enchanting:
gui-item: enchanting_table
# The maximum obtainable level
max-level: 50
name: "Enchanting"
farming:
gui-item: golden_hoe
# The maximum obtainable level
max-level: 50
name: "Farming"
woodcutting:
gui-item: jungle_sapling
# The maximum obtainable level
max-level: 50
name: "Woodcutting"
fishing:
gui-item: fishing_rod
# The maximum obtainable level
max-level: 50
name: "Fishing"
alchemy:
gui-item: brewing_stand
# The maximum obtainable level
max-level: 50
name: "Alchemy"
armory:
gui-item: iron_chestplate
# The maximum obtainable level
max-level: 50
name: "Armory"
exploration:
gui-item: leather_boots
# The maximum obtainable level
max-level: 50
name: "Exploration"
stats:
defense: