Skip to content

Commit

Permalink
Add option for XP steal, closes #219
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanceSD committed Jul 12, 2024
1 parent 3d68b26 commit 5e0b0cb
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.bukkit.entity.Player;

import me.NoChance.PvPManager.Dependencies.Interfaces.Dependency;

/**
* Represents a plugin that groups players in clans/factions/teams/etc. <br>
* Allows checking if two players can attack each other or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

import me.NoChance.PvPManager.Dependencies.BaseDependency;
import me.NoChance.PvPManager.Dependencies.Hook;
import me.chancesd.sdutils.utils.Log;
import me.NoChance.PvPManager.Dependencies.Interfaces.DisguiseDependency;
import me.libraryaddict.disguise.DisguiseAPI;

public class LibsDisguisesHook extends BaseDependency implements DisguiseDependency {

public LibsDisguisesHook(final Hook hook) {
super(hook);
Log.debug(DisguiseAPI.getSelfDisguiseId() + ""); // throw exception if not found
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import me.NoChance.PvPManager.Dependencies.DependencyException;
import me.NoChance.PvPManager.Dependencies.Hook;
import me.NoChance.PvPManager.Dependencies.GroupDependency;
import me.NoChance.PvPManager.Player.CancelResult;
import me.NoChance.PvPManager.Dependencies.ForceToggleDependency;
import me.NoChance.PvPManager.Dependencies.Hook;
import me.NoChance.PvPManager.Dependencies.Interfaces.PvPDependency;
import me.NoChance.PvPManager.Player.ProtectionType;
import net.sacredlabyrinth.phaed.simpleclans.ClanPlayer;
import net.sacredlabyrinth.phaed.simpleclans.SimpleClans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.sk89q.worldguard.protection.flags.StateFlag.State;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

import me.NoChance.PvPManager.Dependencies.GroupDependency;
import me.NoChance.PvPManager.Managers.PlayerHandler;

public interface WorldGuardDependency extends GroupDependency, RegionDependency {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import me.NoChance.PvPManager.Settings.Settings;
import me.NoChance.PvPManager.Utils.CombatUtils;
import me.chancesd.pvpmanager.setting.Permissions;
import me.NoChance.PvPManager.Utils.MCVersion;
import me.chancesd.sdutils.utils.MCVersion;

public class EntityListener implements Listener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public final void onPlayerDeath(final PlayerDeathEvent event) {
// Player died in combat, process that
if (killer != null && !killer.equals(player)) {
final PvPlayer pKiller = playerHandler.get(killer);
handlePvPDeath(player, pvPlayer, killer, pKiller);
handlePvPDeath(player, pvPlayer, killer, pKiller, event);
}

if (pvPlayer.isInCombat()) {
Expand All @@ -170,7 +170,7 @@ public final void onPlayerDeath(final PlayerDeathEvent event) {
playerHandler.handlePlayerDrops(event, player, killer);
}

private void handlePvPDeath(final Player player, final PvPlayer pvPlayer, final Player killer, final PvPlayer pKiller) {
private void handlePvPDeath(final Player player, final PvPlayer pvPlayer, final Player killer, final PvPlayer pKiller, final PlayerDeathEvent event) {
if (Settings.isKillAbuseEnabled() && !pKiller.hasPerm(Permissions.EXEMPT_KILL_ABUSE)) {
pKiller.addVictim(player);
}
Expand All @@ -181,6 +181,12 @@ private void handlePvPDeath(final Player player, final PvPlayer pvPlayer, final
if (Settings.getMoneyPenalty() > 0) {
pvPlayer.applyPenalty();
}
if (Settings.getExpSteal() > 0) {
final int expWon = pKiller.giveExp(pvPlayer);
event.setDroppedExp(0);
event.setNewExp(player.getTotalExperience() - expWon);
pvPlayer.message(Messages.getExpStolen(pKiller.getName(), String.valueOf(expWon)));
}
CombatUtils.executeCommands(Settings.getCommandsOnKill(), killer, killer.getName(), player.getName());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ private boolean withdrawMoney(final double amount) {

private void depositMoney(final double amount) {
final EconomyResponse response = economy.depositPlayer(getPlayer(), amount);
Log.debug("Deposit money to " + getName() + " - Response: " + response.type + " " + response.amount + " " + response.balance + " " + response.errorMessage);
Log.debug("Deposit money to " + getName() + " - Response: " + response.type + " " + response.amount + " " + response.balance + " "
+ response.errorMessage);
}

public final void applyFine() {
Expand Down Expand Up @@ -62,6 +63,25 @@ public final void giveReward(final EcoPlayer victim) {
message(Messages.getMoneyReward(victim.getPlayer().getName(), CombatUtils.formatTo2Digits(moneyWon)));
}

public final int giveExp(final EcoPlayer victim) {
int expWon = 0;
final int exp = victim.getPlayer().getTotalExperience();
if (Settings.getExpSteal() <= 1) {
expWon = (int) (Settings.getExpSteal() * exp);
} else {
expWon = exp;
}
setExp(getPlayer().getTotalExperience() + expWon);
message(Messages.getExpWon(victim.getPlayer().getName(), String.valueOf(expWon)));
return expWon;
}

public final void setExp(final int exp) {
getPlayer().setExp(0);
getPlayer().setLevel(0);
getPlayer().giveExp(exp);
}

private double getMoneyPercentage(final double percentage) {
if (percentage > 1)
return percentage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class Messages {
private static String moneyReward;
private static String moneyPenalty;
private static String moneySteal;
private static String expWon;
private static String expStolen;
private static Locale locale;
private static String tagTimeLeft;
private static String tagNotInCombat;
Expand Down Expand Up @@ -212,6 +214,8 @@ private static void getMessages() {
moneyReward = getString("Money_Reward");
moneyPenalty = getString("Money_Penalty");
moneySteal = getString("Money_Steal");
expWon = getString("Exp_Won");
expStolen = getString("Exp_Stolen");
pvpListTitle = getString("PvPList_Title");
pvpListEnabled = getString("PvPList_Enabled");
pvpListDisabled = getString("PvPList_Disabled");
Expand Down Expand Up @@ -327,6 +331,10 @@ public static String replaceMoney(final String message, final String money) {
return message.replace("%money%", money);
}

public static String replaceExp(final String message, final String exp) {
return message.replace("%exp%", exp);
}

public static String getPrefix() {
return prefix;
}
Expand Down Expand Up @@ -481,6 +489,14 @@ public static String getMoneySteal(final String name, final String money) {
return replaceMoney(replacePlayer(moneySteal, name), money);
}

public static String getExpStolen(final String name, final String exp) {
return replaceExp(replacePlayer(expStolen, name), exp);
}

public static String getExpWon(final String name, final String exp) {
return replaceExp(replacePlayer(expWon, name), exp);
}

public static Locale getLocale() {
return locale;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public enum DropMode {
private static double moneyPenalty;
private static double moneyReward;
private static boolean moneySteal;
private static double expSteal;
private static String nameTagPrefix;
private static String nameTagSuffix;
private static boolean newbieGodMode;
Expand Down Expand Up @@ -171,7 +172,7 @@ public static void initizalizeVariables(final YamlConfiguration c) {
recyclePotionBottles = generalSection.getBoolean("Recycling.Potion Bottle", false);
recycleMilkBucket = generalSection.getBoolean("Recycling.Milk Bucket", false);
healthBelowName = generalSection.getBoolean("Show health under name.Enabled", true);
healthBelowNameSymbol = generalSection.getString("Show health under name.Display Name", "❤");
healthBelowNameSymbol = ChatUtils.colorize(generalSection.getString("Show health under name.Display Name", "&c❤"));
worldsExcluded = new HashSet<>(getList(generalSection.getStringList("World Exclusions")));

borderHoppingVulnerable = borderHoppingSection.getBoolean("Vulnerable", true);
Expand Down Expand Up @@ -258,6 +259,7 @@ public static void initizalizeVariables(final YamlConfiguration c) {
setMoneyReward(playerKillsSection.getDouble("Money Reward", 10));
setMoneyPenalty(playerKillsSection.getDouble("Money Penalty", 10));
moneySteal = playerKillsSection.getBoolean("Money Steal", false);
expSteal = playerKillsSection.getDouble("Exp Steal", 0.0);
commandsOnKill = getCommandList(playerKillsSection.getStringList("Commands On Kill"));
playerKillsWGExclusions = new HashSet<>(getList(playerKillsSection.getStringList("WorldGuard Exclusions")));

Expand Down Expand Up @@ -558,6 +560,10 @@ public static boolean isMoneySteal() {
return moneySteal;
}

public static double getExpSteal() {
return expSteal;
}

public static boolean isUntagEnemy() {
return untagEnemy;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public static boolean isWorldExcluded(final String worldName) {
}

public static boolean hasHarmfulPotion(final AreaEffectCloud areaCloud) {
if (isVersionAtLeast(Settings.getMinecraftVersion(), "1.20.2")) {
if (MCVersion.isAtLeast(MCVersion.V1_20_2)) {
final PotionType basePotionType = areaCloud.getBasePotionType();
if (basePotionType == null)
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import java.util.UUID;

import org.bukkit.ChatColor;
import org.bukkit.scoreboard.Criteria;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.RenderType;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;

Expand Down Expand Up @@ -84,11 +86,15 @@ private void setup() {
// set pvp tag if player has pvp nametags on
setPvP(pvPlayer.hasPvPEnabled());
}
if (Settings.isHealthBelowName() && health == null) {
if (Settings.isHealthBelowName() && (health == null || scoreboard.getObjective(HEALTHOBJ) == null)) {
if (scoreboard.getObjective(HEALTHOBJ) != null) {
health = scoreboard.getObjective(HEALTHOBJ);
} else if (MCVersion.isAtLeast(MCVersion.V1_19)) {
health = scoreboard.registerNewObjective(HEALTHOBJ, Criteria.HEALTH, Settings.getHealthBelowNameSymbol(), RenderType.HEARTS);
health.setDisplaySlot(DisplaySlot.BELOW_NAME);
} else {
health = scoreboard.registerNewObjective(HEALTHOBJ, "health", Settings.getHealthBelowNameSymbol());
health = scoreboard.registerNewObjective(HEALTHOBJ, "health");
health.setDisplayName(Settings.getHealthBelowNameSymbol());
health.setDisplaySlot(DisplaySlot.BELOW_NAME);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected Database(final DatabaseFactory databaseFactory, final DatabaseConfigBu
final HikariConfig config = new HikariConfig();
if (databaseType == DatabaseType.SQLITE) {
// Use SQLITE
if (MCVersion.isLessThan(MCVersion.V1_9)) {
if (MCVersion.isLowerThan(MCVersion.V1_9)) {
try {
Class.forName("org.sqlite.JDBC"); // got to do this for 1.8 sigh
config.setConnectionTestQuery("SELECT 1;");
Expand Down
8 changes: 5 additions & 3 deletions pvpmanager/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
# mode 'TRANSFER' - (Same as 'DROP' but transfer items(not exp) directly to killer's inventory without drops)
# mode 'CLEAR' - (Clear all drops and exp on death, this deletes everything on death so make sure it is what you want)
# Ignore No Damage Hits -> Ignore hits from snowballs, eggs, fishing rod and others
# Show health under name -> Show player health, uses scoreboards so might conflict with other plugins
# World Exclusions -> List of worlds where PvPManager will have no effect
General:
# Changes messages language, options are - EN | BG | DE | ES | FI | FR | HR | IT | JA | KO | PL | pt_BR | RU | TR | zh_TW | ZH
Locale: EN
Expand All @@ -33,9 +31,11 @@ General:
Recycling:
Potion Bottle: false
Milk Bucket: false
# Show player health, uses scoreboards so might conflict with other plugins
Show health under name:
Enabled: true
Display Name: ''
Display Name: '&c❤'
# List of worlds where PvPManager will have no effect
World Exclusions:
- 'example'

Expand Down Expand Up @@ -198,6 +198,8 @@ Player Kills:
Money Reward: 0.0
Money Penalty: 0.0
Money Steal: false
# Similar to how money steal works, get a percentage of XP from the player you killed, 1 for 100% (0 to disable)
Exp Steal: 0.0
# Commands to execute on kill (<player> is the killer, <victim> is the victim, <item> is the item material in hand)
Commands On Kill:
- 'examplecommand <player> killed <victim> with <item>'
Expand Down
8 changes: 5 additions & 3 deletions pvpmanager/src/main/resources/locale/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,17 @@ Respawn_Protection_Other = %prefix%&4 %player% has respawn protection!
World_Protection = %prefix% &4PvP is disabled in this world!

# AFK Protection
AFK_Protection = &6[&8PvPManager&6]&4 This player can't be attacked while AFK
AFK_Protection = %prefix% &4This player can't be attacked while AFK

# Global Protection
Global_Protection = &6[&8PvPManager&6]&4 PvP combat is globally disabled
Global_Protection = %prefix% &4PvP combat is globally disabled

# Player Kills
Money_Reward = &2You got %money% coins for killing player %player%!
Money_Penalty = &cYou lost %money% coins for getting killed!
Money_Steal = &c%player% stole %money% coins from you!
Exp_Won = %prefix% &aYou got &e%exp% &aexperience for killing &c%player%&a!
Exp_Stolen = %prefix% &c%player% &6stole &e%exp% &6experience from you!

# Kill Abuse
Kill_Abuse_Warning = %prefix% &cYou will be punished if you kill this player again! Please stop.
Expand All @@ -81,7 +83,7 @@ Error_Not_Newbie = %prefix% &cYou are not a newbie!
Error_PvP_Cooldown = %prefix% &cYou can't toggle PvP yet! Wait %time%.
Error_PvPToggle_NoPvP = %prefix% &cYou are blocked from having PvP enabled
Error_PvPToggle_ForcePvP = %prefix% &cYou are blocked from having PvP disabled
PvPToggle_Admin_Changed = &6[&8PvPManager&6] &2PvP state for &6%p &2was changed to %state
PvPToggle_Admin_Changed = %prefix% &2PvP state for &6%p &2was changed to %state
PvPToggle_Already_Disabled = %prefix% &4You already have PvP disabled!
PvPToggle_Already_Enabled = %prefix% &4You already have PvP enabled!
# PvPInfo Command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.AdditionalMatchers;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;

Expand All @@ -35,7 +36,7 @@ public static void setupClass() {
void allEnabled() {
final JavaPlugin plugin = Mockito.mock(JavaPlugin.class);
when(plugin.getDescription()).thenReturn(new PluginDescriptionFile("Plugin", "1.0", "plugin"));
when(server.getPluginManager().getPlugin(ArgumentMatchers.anyString())).thenReturn(plugin);
when(server.getPluginManager().getPlugin(AdditionalMatchers.not(ArgumentMatchers.eq("Towny")))).thenReturn(plugin);
assertEquals(server.getPluginManager().getPlugin("PvPManager"), plugin);
new DependencyManager();
}
Expand Down

0 comments on commit 5e0b0cb

Please sign in to comment.