Skip to content

Commit

Permalink
1.4.2 - World and coordinates placeholders, fix some replace edge cases
Browse files Browse the repository at this point in the history
- Version 1.4.2

- The world and coordinates of a killed dragon can now be included in
the kill reward commands. Issue #2

- Use replaceAll instead of replace for filling placeholders
  • Loading branch information
hraponssi committed Dec 12, 2023
1 parent 1ef116a commit a2ae8df
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>DragonDmg</groupId>
<artifactId>DragonDmg</artifactId>
<version>1.4.1</version>
<version>1.4.2</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -35,7 +35,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<version>1.20.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
47 changes: 32 additions & 15 deletions src/main/java/net/hraponssi/dragondmg/main/EventHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.entity.Arrow;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -37,23 +38,39 @@ public void onEntityDeath(EntityDeathEvent e) { //Dragon death
if (type.equals(EntityType.ENDER_DRAGON)) {
if (e.getEntity().getKiller().getType().equals(EntityType.PLAYER)) {
double total = 0;
Location loc = e.getEntity().getLocation();
String cleanedWorldName = loc.getWorld().getName();
String defaultPrefix = Bukkit.getWorlds().get(0).getName();
if (plugin.executeFriendlyNames) {
// Vanilla Minecraft execute commands don't include the default world name like Spigot does, instead using "overworld", "the_end", etc
// Because of this the overworld, nether, and end have special cases:
if (cleanedWorldName.equals(defaultPrefix)) {
cleanedWorldName = "overworld";
} else if (cleanedWorldName.equals(defaultPrefix + "_the_end")) {
cleanedWorldName = cleanedWorldName.replace(defaultPrefix + "_", "");
} else if (cleanedWorldName.equals(defaultPrefix + "_the_nether")) {
cleanedWorldName = cleanedWorldName.replace(defaultPrefix + "_", "");
}
}
for (Entry<String, Double> entry : dragonDamage.entrySet()) {
total += entry.getValue();
}
String[] tsplit = Double.toString(total).split("\\.");
if (plugin.announceKiller) {
plugin.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.killerMsg.replace("%killer%", e.getEntity().getKiller().getName())));
plugin.killerMsg.replaceAll("%killer%", e.getEntity().getKiller().getName())));
}
if (plugin.killReward) {
for (String reward : plugin.killRewards) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
reward.replace("%killer%", e.getEntity().getKiller().getName()));
reward.replaceAll("%killer%", e.getEntity().getKiller().getName())
.replaceAll("%world%", cleanedWorldName)
.replaceAll("%coordinates%", loc.getBlockX() + " " + loc.getBlockY() + " " + loc.getBlockZ()));
}
}
plugin.sendMessage(ChatColor
.translateAlternateColorCodes('&', plugin.dmgListTitle.replace("%totaldmg%", tsplit[0]))
.replace("%killer%", e.getEntity().getKiller().getName()));
.translateAlternateColorCodes('&', plugin.dmgListTitle.replaceAll("%totaldmg%", tsplit[0]))
.replaceAll("%killer%", e.getEntity().getKiller().getName()));
List<Double> dmgOrder = dragonDamage.values().stream().distinct().collect(Collectors.toList()); //List of all distinct damage values
dragonDamage.keySet().stream().forEach(name -> crystalKills.putIfAbsent(name, 0));
Collections.sort(dmgOrder, new Comparator<Double>() { //Sort list biggest to smallest
Expand All @@ -62,15 +79,15 @@ public int compare(Double db1, Double db2) {
}
});
for (double dmglevel : dmgOrder) { // Iterate over the damage values in order
for (Entry<String, Double> entry : dragonDamage.entrySet()) { //Print out players who's damage matches the damage value being processed
for (Entry<String, Double> entry : dragonDamage.entrySet()) { // Print out players who's damage matches the damage value being processed
double dmg = entry.getValue();
String cleandmg = Double.toString(dmg).split("\\.")[0];
if (dmg == dmglevel) {
plugin.sendMessage(ChatColor.translateAlternateColorCodes('&',
plugin.dmgListEntry.replace("%player%", entry.getKey()).replace("%dmg%", cleandmg)
.replace("%totaldmg%", tsplit[0])
.replace("%crystals%", crystalKills.get(entry.getKey()).toString())
.replace("%killer%", e.getEntity().getKiller().getName())));
plugin.dmgListEntry.replaceAll("%player%", entry.getKey()).replaceAll("%dmg%", cleandmg)
.replaceAll("%totaldmg%", tsplit[0])
.replaceAll("%crystals%", crystalKills.get(entry.getKey()).toString())
.replaceAll("%killer%", e.getEntity().getKiller().getName())));
Random random = new Random();
float dmgp = (Float.parseFloat(cleandmg) / Float.parseFloat(tsplit[0])) * 100;
// Rewards
Expand All @@ -83,20 +100,20 @@ public int compare(Double db1, Double db2) {
if (player != null) {
if (plugin.damageXPMultiplier) {
float xp = ((float) plugin.baseXP) * (Math.min(dmgp+boost, 100) / 100f);
plugin.getLogger().info("agive xp " + player.getName() + " " + xp);
// plugin.getLogger().info("agive xp " + player.getName() + " " + xp);
player.giveExp(Math.round(xp));
} else {
player.giveExp(plugin.baseXP);
}
}
}
if (plugin.dmgReward && random.nextInt(100) + 1 <= Math.min(dmgp+boost, 100)) { //if dmg percent equal or more than random 1-100 number
if (plugin.dmgReward && random.nextInt(100) + 1 <= Math.min(dmgp+boost, 100)) { // if dmg percent equal or more than random 1-100 number
for (String reward : plugin.dmgRewards) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
reward.replace("%player%", entry.getKey()).replace("%dmg%", cleandmg)
.replace("%totaldmg%", tsplit[0])
.replace("%crystals%", crystalKills.get(entry.getKey()).toString())
.replace("%killer%", e.getEntity().getKiller().getName()));
reward.replaceAll("%player%", entry.getKey()).replaceAll("%dmg%", cleandmg)
.replaceAll("%totaldmg%", tsplit[0])
.replaceAll("%crystals%", crystalKills.get(entry.getKey()).toString())
.replaceAll("%killer%", e.getEntity().getKiller().getName()));
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/hraponssi/dragondmg/main/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Main extends JavaPlugin implements Listener {
boolean crystalRewardBoost;
boolean killReward;
boolean dmgReward;
boolean executeFriendlyNames;

String dmgListTitle;
String dmgListEntry;
Expand Down Expand Up @@ -60,6 +61,7 @@ public void loadConfig() {
killerMsg = config.getString("killermsg");
killRewards = config.getStringList("killrewards");
dmgRewards = config.getStringList("dmgrewards");
executeFriendlyNames = config.getBoolean("executefriendlyworldnames");
}

public void sendMessage(String msg) {
Expand Down
9 changes: 6 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ basexp: 100
crystalrewardboost: false
crystalboostpercent: 5

# Award for the dragon killer.
# Commands to be run when the dragon is killed, e.g. killer awards or item drops. placeholders: %killer% %coordinates% %world%
# The world name can be either the spigot name, or vanilla /execute command friendly name (spigot: "world_the_end", vanilla: "the_end")
enablekillerrewards: false
executefriendlyworldnames: true
killrewards:
- "give %killer% dragon_egg 1"
- 'give %killer% dragon_egg 1'
- 'execute in minecraft:%world% run summon item %coordinates% {Item:{id:"minecraft:elytra",Count:1}}'

# Dmg awards have a chance relative to % dmg done to the dragon (eg 20 out of 100 dmg is a 20% chance).
enabledmgrewards: false
dmgrewards:
- "give %player% elytra 1"
- 'give %player% elytra 1'

0 comments on commit a2ae8df

Please sign in to comment.