Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.kgaz.exp;
- import java.io.File;
- import java.io.IOException;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Locale;
- import org.bukkit.Bukkit;
- import org.bukkit.configuration.file.YamlConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.scheduler.BukkitRunnable;
- import me.kgaz.Main;
- import net.md_5.bungee.api.ChatMessageType;
- import net.md_5.bungee.api.chat.TextComponent;
- public class PlayerData {
- public static final float LEVELUPMULTIPLIER = 1.5f;
- public static final List<Integer> levelsToShow = Arrays.asList(1,3);
- private Player owner;
- private String playerName;
- private YamlConfiguration yml;
- private String path;
- private File file;
- public void init() {
- owner = Bukkit.getPlayer(playerName);
- }
- public PlayerData(YamlConfiguration yml, String path, String playerName, File file) {
- this.yml = yml;
- this.path = path;
- this.playerName = playerName;
- init();
- }
- public void clearData() {
- yml.set(path+".level", 0);
- yml.set(path+".exp", 0.0f);
- yml.set(path+".needExp", 0);
- saveFile();
- }
- private void saveFile() {
- try {
- yml.save(file);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public void setLevel(Integer level) {
- init();
- yml.set(path+".level", level);
- saveFile();
- }
- public void removeExp(Float amount) {
- init();
- Float exp = (float) yml.getDouble(path+".exp");
- exp-=amount;
- if(exp<0) exp=0.0f;
- yml.set(path+".exp", exp);
- saveFile();
- checkLevel();
- }
- public void setExp(Float amount) {
- init();
- yml.set(path+".exp", amount);
- saveFile();
- checkLevel();
- }
- public void addExp(Float amount) {
- init();
- yml.set(path+".exp", Float.parseFloat(String.format(Locale.US, "%.1f", yml.getDouble(path+".exp")+amount)));
- // int gainedLevels = 0;
- // while(exp>needExp) {
- // exp-=needExp;
- // needExp=(int) Math.ceil(needExp*LEVELUPMULTIPLIER);
- // gainedLevels++;
- // }
- // if(gainedLevels==1) {
- // levelUp();
- // owner.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e§oZdobyles exp! §6§oNowy Poziom!"));
- // } else if (gainedLevels>1) {
- // levelUp();
- // owner.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e§oZdobyles exp! §6§oNowy Poziom!"));
- // } else {
- // owner.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e§oZdobyles exp! §f§o"+String.format(Locale.US, "%.1f", exp)+"§6§o/§e§o"+needExp));
- // }
- saveFile();
- checkLevel();
- }
- public void checkLevel() {
- init();
- Float exp = (float) yml.getDouble(path+".exp");
- Integer needExp = yml.getInt(path+".needExp");;
- Integer level = yml.getInt(path+".level");;
- int gainedLevels = 0;
- while(exp>needExp) {
- exp-=needExp;
- needExp=(int) Math.ceil(needExp*LEVELUPMULTIPLIER);
- gainedLevels++;
- }
- if(gainedLevels==1) {
- levelUp(level);
- owner.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e§oZdobyles exp! §6§oNowy Poziom!"));
- } else if (gainedLevels>1) {
- levelUp(level);
- owner.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e§oZdobyles exp! §6§oNowy Poziom!"));
- } else {
- owner.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText("§e§oZdobyles exp! §f§o"+String.format(Locale.US, "%.1f", exp)+"§6§o/§e§o"+needExp));
- }
- yml.set(path+".exp", exp);
- yml.set(path+".needExp", needExp);
- yml.set(path+".level", level);
- }
- public void levelUp(Integer level) {
- {
- {//LEVELUP STUFF
- if(levelsToShow.contains(level) || level % 5 == 0) {
- Bukkit.broadcastMessage("§8[§4§lLVL§8] §7Gracz §c"+owner.getName()+" §7osiagnal poziom §4"+level+"§7! Gratulacje!");
- }
- {
- new BukkitRunnable() {
- public void run() {
- owner.getWorld().strikeLightningEffect(owner.getLocation());
- }
- }.runTaskLater(Main.getInst(), 1);
- new BukkitRunnable() {
- public void run() {
- owner.getWorld().strikeLightningEffect(owner.getLocation());
- }
- }.runTaskLater(Main.getInst(), 8);
- new BukkitRunnable() {
- public void run() {
- owner.getWorld().strikeLightningEffect(owner.getLocation());
- }
- }.runTaskLater(Main.getInst(), 20);
- }
- }
- }
- }
- public Integer getLevel() {
- return level;
- }
- public Float getExp() {
- return exp;
- }
- public Integer getNeedExp() {
- return needExp;
- }
- public Player getOwner() {
- return owner;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement