Skip to content

Commit

Permalink
kill/death messages added + low needed exp fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlexeiMK committed Aug 4, 2023
1 parent 03aa59c commit f06a8b6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command

HSLevels.getInstance().config().reloadConfig();
HSLevels.getInstance().getMessage().reloadConfig();
HSLevels.getInstance().config().saveConfig();
HSLevels.getInstance().getMessage().saveConfig();

MessageConstructor
.of(Message.SUCCESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public class Message extends AbstractConfig {
public static String COMMON_PLUS_XP;
public static String COMMON_MINUS_XP;
public static String COMMON_XP_BAR;
public static String COMMON_PVP_KILL;
public static String COMMON_PVP_DEATH;
public static String HELP_LEVEL;
public static String HELP_LEVEL_PLAYER;
public static String HELP_ADD_LEVEL;
Expand Down Expand Up @@ -43,6 +45,8 @@ public void init() {
COMMON_PLUS_XP = getString("common.plus_xp");
COMMON_MINUS_XP = getString("common.minus_xp");
COMMON_XP_BAR = getString("common.xp_bar");
COMMON_PVP_KILL = getString("common.pvp_kill");
COMMON_PVP_DEATH = getString("common.pvp_death");
HELP_LEVEL = getString("help.level");
HELP_LEVEL_PLAYER = getString("help.level-player");
HELP_ADD_LEVEL = getString("help.add-level");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,60 +48,57 @@ public int getVanillaExpToUp(int level) {
}

public void giveLevels(Player p, int levels) {
if(p.getLevel() > config.getMaxLevel()) {
if (p.getLevel() > config.getMaxLevel()) {
p.setLevel(config.getMaxLevel());
p.setExp(0f);
return;
}
p.giveExpLevels(p.getLevel() + levels > config.getMaxLevel() ?
config.getMaxLevel() - p.getLevel() : levels);
if(p.getLevel() == config.getMaxLevel()) p.setExp(0f);
if (p.getLevel() == config.getMaxLevel()) p.setExp(0f);
}

public void giveExp(Player p, int amount) {
if(p.getLevel() >= config.getMaxLevel()) {
if (p.getLevel() >= config.getMaxLevel()) {
p.setLevel(config.getMaxLevel());
p.setExp(0f);
return;
}

int lvlRequiredExp = config.getXpToUp(p.getLevel() + 1);
int exp = getExp(p);
if(exp + amount > lvlRequiredExp) {
if (exp + amount > lvlRequiredExp) {
p.setExp(0);
p.setLevel(p.getLevel() + 1);
giveExp(p, exp + amount - lvlRequiredExp);
}
else {
} else {
p.setExp(Math.max(0, Math.min(1, p.getExp() +
(float) amount / config.getXpToUp(p.getLevel() + 1))));
}
}

public void takeExp(Player p, int amount) {
if(amount > getExp(p)) {
if (amount > getExp(p)) {
p.setExp(0);
if(p.getLevel() != 0) {
if (p.getLevel() != 0) {
p.setLevel(p.getLevel() - 1);
p.setExp(1);
takeExp(p, amount - getExp(p));
}
}
else {
} else {
p.setExp(Math.max(0, Math.min(1, p.getExp() -
(float) amount / config.getXpToUp(p.getLevel() + 1))));
}
}

public void alertExp(Player p, int amount) {
if(amount > 0) {
if (amount > 0) {
p.sendActionBar(Component.text(MessageConstructor
.of(Message.COMMON_PLUS_XP)
.replace("%xp%", String.valueOf(amount))
.get()
));
}
else if(amount < 0) {
} else if (amount < 0) {
p.sendActionBar(Component.text(MessageConstructor
.of(Message.COMMON_MINUS_XP)
.replace("%xp%", String.valueOf(-amount))
Expand All @@ -124,24 +121,20 @@ public int toVanillaExp(Player p, int amount) {
public void onExpChange(PlayerExpChangeEvent e) {
Player p = e.getPlayer();
int amount = e.getAmount();
if(amount <= 0) return;
if (amount <= 0) return;
e.setAmount(0);

if(p.getLevel() > config.getMaxLevel()) {
if (p.getLevel() > config.getMaxLevel()) {
p.setLevel(config.getMaxLevel());
p.setExp(0f);
return;
}
if(p.getLevel() == config.getMaxLevel() && p.getExp() > 0f) {
if (p.getLevel() == config.getMaxLevel() && p.getExp() > 0f) {
p.setExp(0f);
return;
}

int neededExp = config.getXpToUp(p.getLevel() + 1);
if(neededExp == 0) return;

p.setExp(Math.min(1, p.getExp() + (float) amount / neededExp));
if(p.getExp() == 1f) p.giveExp(1);
giveExp(p, amount);

p.sendActionBar(Component.text(MessageConstructor
.of(Message.COMMON_PLUS_XP)
Expand Down Expand Up @@ -186,18 +179,28 @@ public void onDeath(PlayerDeathEvent e) {
int xp = Math.max(0, oldExp - getExp(p));
giveExp(p.getKiller(), xp);

if(xp == 0) return;
if (xp == 0) return;
p.sendActionBar(Component.text(MessageConstructor
.of(Message.COMMON_MINUS_XP)
.replace("%xp%", String.valueOf(xp))
.get()
));
MessageConstructor
.of(Message.COMMON_PVP_DEATH)
.replace("%player", p.getKiller().getName())
.replace("%xp%", String.valueOf(xp))
.send(p);

p.getKiller().sendActionBar(Component.text(MessageConstructor
.of(Message.COMMON_PLUS_XP)
.replace("%xp%", String.valueOf(xp))
.get()
));
MessageConstructor
.of(Message.COMMON_PVP_KILL)
.replace("%player", p.getName())
.replace("%xp%", String.valueOf(xp))
.send(p.getKiller());
}
}
}
10 changes: 10 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ common:
plus_xp: "+ %xp%xp"
minus_xp: "- %xp%xp"
xp_bar: "||||||||||||||||||||"
pvp_kill:
- ''
- 'Вы убили игрока %player% и получили'
- 'за это %xp% опыта'
- ''
pvp_death:
- ''
- 'Вы были убиты игроком %player% и '
- 'потеряли %xp% опыта, теперь у вас'
- ''
help:
level: "/level"
level-player: "/level <player>"
Expand Down
10 changes: 10 additions & 0 deletions target/classes/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ common:
plus_xp: "+ %xp%xp"
minus_xp: "- %xp%xp"
xp_bar: "||||||||||||||||||||"
pvp_kill:
- ''
- 'Вы убили игрока %player% и получили'
- 'за это %xp% опыта'
- ''
pvp_death:
- ''
- 'Вы были убиты игроком %player% и '
- 'потеряли %xp% опыта, теперь у вас'
- ''
help:
level: "/level"
level-player: "/level <player>"
Expand Down

0 comments on commit f06a8b6

Please sign in to comment.