Skip to content

Commit

Permalink
Rework oldpvp flag handling
Browse files Browse the repository at this point in the history
  • Loading branch information
imDaniX committed Aug 8, 2024
1 parent 10c884d commit add0e76
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/wgextender/WGExtender.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void onDisable() {
WEWandCommandWrapper.uninject();
WGRegionCommandWrapper.uninject();
pvplistener.uninject();
oldpvphandler.stop();
oldpvphandler.stop(this);
} catch (Throwable t) {
getLogger().log(Level.SEVERE, "Unable to uninject, shutting down", t);
Bukkit.shutdown();
Expand Down
51 changes: 27 additions & 24 deletions src/wgextender/features/flags/OldPVPFlagsHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package wgextender.features.flags;

import com.google.common.base.Function;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Server;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import org.bukkit.entity.Entity;
Expand All @@ -15,6 +13,7 @@
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDamageEvent.DamageModifier;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.plugin.Plugin;
Expand All @@ -27,7 +26,6 @@
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

@Deprecated
Expand All @@ -51,29 +49,23 @@ public void start(Plugin plugin) {
return;
}

Server server = plugin.getServer();
server.getPluginManager().registerEvents(this, plugin);
server.getAsyncScheduler().runAtFixedRate(plugin, (task) -> {
for (Player player : Bukkit.getOnlinePlayers()) {
if (WGRegionUtils.isFlagTrue(player.getLocation(), WGExtenderFlags.OLDPVP_ATTACKSPEED)) {
if (oldValues.containsKey(player.getUniqueId())) continue;
player.getScheduler().run(WGExtender.getInstance(), (playerTask) -> {
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
oldValues.put(player.getUniqueId(), attribute.getBaseValue());
attribute.setBaseValue(16.0);
}, null);
} else {
player.getScheduler().run(WGExtender.getInstance(), (playerTask) -> {
reset(player);
}, null);
}
}
}, 0, 50, TimeUnit.MICROSECONDS);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}

public void stop() {
for (Player player : Bukkit.getOnlinePlayers()) {
player.getScheduler().run(WGExtender.getInstance(), playerTask -> reset(player), null);
public void stop(Plugin plugin) {
for (Player player : plugin.getServer().getOnlinePlayers()) {
reset(player);
}
}

private void handlePlayer(Player player) {
if (WGRegionUtils.isFlagTrue(player.getLocation(), WGExtenderFlags.OLDPVP_ATTACKSPEED)) {
if (oldValues.containsKey(player.getUniqueId())) return;
AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED);
oldValues.put(player.getUniqueId(), attribute.getBaseValue());
attribute.setBaseValue(16.0);
} else {
reset(player);
}
}

Expand All @@ -84,6 +76,17 @@ private void reset(Player player) {
}
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
handlePlayer(player);
player.getScheduler().runAtFixedRate(WGExtender.getInstance(),
(task) -> handlePlayer(player),
() -> reset(player),
1, 1
);
}

@EventHandler(priority = EventPriority.LOWEST)
public void onQuit(PlayerQuitEvent event) {
reset(event.getPlayer());
Expand Down

0 comments on commit add0e76

Please sign in to comment.