Initial Commit
This commit is contained in:
110
src/main/java/me/miningtcup/directXP/DirectXP.java
Normal file
110
src/main/java/me/miningtcup/directXP/DirectXP.java
Normal file
@@ -0,0 +1,110 @@
|
||||
package me.miningtcup.directXP;
|
||||
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.BlockBreakEvent;
|
||||
import org.bukkit.event.entity.EntityDeathEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.bukkit.Bukkit.getPluginManager;
|
||||
import static org.bukkit.Bukkit.getScheduler;
|
||||
|
||||
public final class DirectXP extends JavaPlugin implements Listener {
|
||||
private final FileConfiguration config = getConfig();
|
||||
private final Map<Player, Integer> remainingDings = new HashMap<>();
|
||||
private final Map<Player, Boolean> isPlaying = new HashMap<>();
|
||||
private final Map<Player, Long> lastDingTime = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
this.getLogger().info("DirectXP is enabled.");
|
||||
getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onEntityDeath(EntityDeathEvent event) {
|
||||
reloadConfig();
|
||||
if (!config.getBoolean("entity-death", true)) return;
|
||||
if (event.getDamageSource().getCausingEntity() instanceof Player player) {
|
||||
int exp = event.getDroppedExp();
|
||||
player.giveExp(exp, true);
|
||||
event.setDroppedExp(0);
|
||||
ding(player, exp);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onBlockBreak(BlockBreakEvent event) {
|
||||
reloadConfig();
|
||||
if (!config.getBoolean("break-block", true)) return;
|
||||
int exp = event.getExpToDrop();
|
||||
if (exp > 0) {
|
||||
Player player = event.getPlayer();
|
||||
player.giveExp(exp, true);
|
||||
event.setExpToDrop(0);
|
||||
ding(player, exp);
|
||||
}
|
||||
}
|
||||
|
||||
public void ding(Player player, int exp) {
|
||||
reloadConfig();
|
||||
if (config.getBoolean("multi-sounds")) {
|
||||
int newDings = calculateOrbs(exp).size();
|
||||
remainingDings.put(player, remainingDings.getOrDefault(player, 0) + newDings);
|
||||
startPlaying(player);
|
||||
} else {
|
||||
reloadConfig();
|
||||
long currentTime = System.currentTimeMillis();
|
||||
int soundDelayMillis = config.getInt("sound-delay", 2) * 50;
|
||||
long lastTime = lastDingTime.getOrDefault(player, 0L);
|
||||
|
||||
if (currentTime - lastTime >= soundDelayMillis) {
|
||||
lastDingTime.put(player, currentTime);
|
||||
player.playSound(player.getLocation(),
|
||||
Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.1F, (float) (0.55 + Math.random() * 0.7));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void startPlaying(Player player) {
|
||||
reloadConfig();
|
||||
if (isPlaying.getOrDefault(player, false)) return;
|
||||
isPlaying.put(player, true);
|
||||
|
||||
getScheduler().runTaskTimer(this, task -> {
|
||||
int dingsLeft = remainingDings.getOrDefault(player, 0);
|
||||
if (dingsLeft > 0) {
|
||||
player.playSound(player.getLocation(),
|
||||
Sound.ENTITY_EXPERIENCE_ORB_PICKUP, 0.1F, (float) (0.55 + Math.random() * 0.7));
|
||||
remainingDings.put(player, dingsLeft - 1);
|
||||
} else {
|
||||
task.cancel();
|
||||
isPlaying.put(player, false);
|
||||
}
|
||||
}, 0, config.getInt("sound-delay", 2));
|
||||
}
|
||||
|
||||
private static final int[] ORB_VALUES = {2477, 1237, 617, 307, 149, 73, 37, 17, 7, 3, 1};
|
||||
|
||||
public static List<Integer> calculateOrbs(int totalExperience) {
|
||||
List<Integer> orbs = new ArrayList<>();
|
||||
|
||||
for (int value : ORB_VALUES) {
|
||||
while (totalExperience >= value) {
|
||||
totalExperience -= value;
|
||||
orbs.add(value);
|
||||
}
|
||||
}
|
||||
|
||||
return orbs;
|
||||
}
|
||||
}
|
11
src/main/resources/config.yml
Normal file
11
src/main/resources/config.yml
Normal file
@@ -0,0 +1,11 @@
|
||||
# Should DirectXP apply to breaking blocks?
|
||||
break-block: true
|
||||
|
||||
# Should DirectXP apply to killing entities?
|
||||
entity-death: true
|
||||
|
||||
# Should multiple sounds be played if it's calculated the amount of exp dropped would have resulted in multiple orbs?
|
||||
multi-sounds: true
|
||||
|
||||
# How many ticks between sounds? 2 is vanilla.
|
||||
sound-delay: 2
|
6
src/main/resources/plugin.yml
Normal file
6
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,6 @@
|
||||
name: DirectXP
|
||||
version: '1.1'
|
||||
main: me.miningtcup.directXP.DirectXP
|
||||
api-version: '1.21'
|
||||
authors: [ MiningTcup ]
|
||||
description: Collect XP directly, instead of from orbs.
|
Reference in New Issue
Block a user