Skip to content

Commit

Permalink
Simplify attack cooldown module
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Mar 16, 2024
1 parent e6b94b1 commit a196e6e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package kernitus.plugin.OldCombatMechanics.module;

import kernitus.plugin.OldCombatMechanics.OCMMain;
import kernitus.plugin.OldCombatMechanics.utilities.Messenger;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
Expand All @@ -17,15 +16,13 @@
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
* Disables the attack cooldown.
*/
public class ModuleAttackCooldown extends OCMModule {

private final double NEW_ATTACK_SPEED = 4;

public ModuleAttackCooldown(OCMMain plugin) {
super(plugin, "disable-attack-cooldown");
}
Expand All @@ -47,7 +44,7 @@ public void onWorldChange(PlayerChangedWorldEvent e) {

@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerQuit(PlayerQuitEvent e) {
setAttackSpeed(e.getPlayer(), PVPMode.NEW_PVP.getBaseAttackSpeed());
setAttackSpeed(e.getPlayer(), NEW_ATTACK_SPEED);
}

/**
Expand All @@ -58,13 +55,13 @@ public void onPlayerQuit(PlayerQuitEvent e) {
private void adjustAttackSpeed(Player player) {
final double attackSpeed = isEnabled(player)
? module().getDouble("generic-attack-speed")
: PVPMode.NEW_PVP.getBaseAttackSpeed();
: NEW_ATTACK_SPEED;

setAttackSpeed(player, attackSpeed);
}

@Override
public void onModesetChange(Player player){
public void onModesetChange(Player player) {
adjustAttackSpeed(player);
}

Expand All @@ -74,78 +71,17 @@ public void onModesetChange(Player player){
* @param player the player to set it for
* @param attackSpeed the attack speed to set it to
*/
public static void setAttackSpeed(Player player, double attackSpeed) {
public void setAttackSpeed(Player player, double attackSpeed) {
final AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
if (attribute == null) return;

final double baseValue = attribute.getBaseValue();

if (baseValue != attackSpeed) {
Messenger.debug(String.format("Setting attack speed for player %s to %.2f (was: %.2f)", player.getName(), attackSpeed, baseValue));
debug(String.format("Setting attack speed to %.2f (was: %.2f)", attackSpeed, baseValue), player);

attribute.setBaseValue(attackSpeed);
player.saveData();
}
}

public static void setAttackSpeed(Player player, PVPMode mode) {
setAttackSpeed(player, mode.getBaseAttackSpeed());
}

/**
* The different pvp modes for 1.8 or newer.
*/
public enum PVPMode {
// 40 is needed for no cooldown whatsoever
OLD_PVP("1.8", 40),
NEW_PVP("1.9+", 4);

private final String name;
private final double baseAttackSpeed;

PVPMode(String name, double baseAttackSpeed) {
this.name = name;
this.baseAttackSpeed = baseAttackSpeed;
}

/**
* Returns the human-readable name of the mode.
*
* @return the human-readable name
*/
public String getName() {
return name;
}

/**
* The {@link Attribute#GENERIC_ATTACK_SPEED} base value.
* <p>
* The value might be an approximation, if the attribute does not exist in the PVP mode.
*
* @return the base value
*/
public double getBaseAttackSpeed() {
return baseAttackSpeed;
}

/**
* Returns the PVP mode for the player, defaulting to {@link #OLD_PVP}.
*
* @param player the player to get it for
* @return the PVP mode of the player
*/
public static PVPMode getModeForPlayer(Player player) {
Objects.requireNonNull(player, "Player cannot be null!");

final double baseAttackSpeed = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED).getBaseValue();

return getByBaseAttackSpeed(baseAttackSpeed).orElse(PVPMode.OLD_PVP);
}

private static Optional<PVPMode> getByBaseAttackSpeed(double speed) {
return Arrays.stream(values())
.filter(pvpMode -> pvpMode.getBaseAttackSpeed() == speed)
.findFirst();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ public void onEntityDamage(EntityDamageEvent event) {
@EventHandler(priority = EventPriority.MONITOR)
public void afterEntityDamage(EntityDamageEvent event) {
final Entity damagee = event.getEntity();
debug("monitor DAMAGE to " + damagee.getName() + " CAUSE: " + event.getCause());

if (event instanceof EntityDamageByEntityEvent) {
if (lastDamages.containsKey(damagee.getUniqueId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void setModesetForWorld(UUID worldId, String modeset) {

public static PlayerData fromDocument(Document doc) {
final PlayerData playerData = new PlayerData();
Document modesetByWorldDoc = (Document) doc.get("modesetByWorld");
final Document modesetByWorldDoc = (Document) doc.get("modesetByWorld");
if (modesetByWorldDoc != null) {
for (Map.Entry<String, Object> entry : modesetByWorldDoc.entrySet()) {
UUID worldId = UUID.fromString(entry.getKey());
Expand Down

0 comments on commit a196e6e

Please sign in to comment.