Trial by combat

This commit is contained in:
Esophose 2020-01-16 15:28:45 -07:00
parent 8a5479ec54
commit cc46c295e4
12 changed files with 139 additions and 15 deletions

View file

@ -3,12 +3,12 @@
* + Add ability to create/manage fixed effects from the GUI
* * Convert fixed effect ids into names
* + Add effect/style settings folder that lets you disable effects/style and edit style properties
* + Add setting to disable particles while in combat
*/
/*
* + Added setting to disable particles while in combat
* * /ppo now uses your permissions instead of the player you are targetting
* + Add effect/style name customization through config files
* + Added effect/style name customization through config files
* * Fixed the 'swords' style so you have to be holding a sword/trident
* * Fixed several styles ignoring the disabled worlds setting
* + Added a setting 'dust-size' to change the size of dust particles in 1.13+
@ -53,7 +53,8 @@ import dev.esophose.playerparticles.manager.ParticleManager;
import dev.esophose.playerparticles.manager.ParticleStyleManager;
import dev.esophose.playerparticles.manager.PermissionManager;
import dev.esophose.playerparticles.manager.PluginUpdateManager;
import dev.esophose.playerparticles.particles.PPlayerMovementListener;
import dev.esophose.playerparticles.particles.listener.PPlayerCombatListener;
import dev.esophose.playerparticles.particles.listener.PPlayerMovementListener;
import dev.esophose.playerparticles.util.Metrics;
import java.io.File;
import java.util.LinkedHashMap;
@ -91,6 +92,7 @@ public class PlayerParticles extends JavaPlugin {
PluginManager pm = Bukkit.getPluginManager();
pm.registerEvents(new PPlayerMovementListener(), this);
pm.registerEvents(new PPlayerCombatListener(), this);
pm.registerEvents(new PlayerChatHook(), this);
if (Setting.SEND_METRICS.getBoolean())

View file

@ -17,7 +17,6 @@ import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
public class GuiInventoryLoadPresetGroups extends GuiInventory {
@ -29,8 +28,6 @@ public class GuiInventoryLoadPresetGroups extends GuiInventory {
this.fillBorder(BorderColor.GREEN);
Player player = pplayer.getPlayer();
int index = 10;
int nextWrap = 17;
int maxIndex = 43;

View file

@ -41,6 +41,8 @@ public class ConfigurationManager extends Manager {
GUI_BUTTON_SOUND("gui-button-sound", true, "If clicking a GUI button should make a noise"),
TOGGLE_ON_MOVE("toggle-on-move", false, "If true, styles will not display while the player is moving", "They will instead have the effect displayed at their feet", "Note: Not all styles abide by this rule, but most will"),
TOGGLE_ON_MOVE_DELAY("toggle-on-move-delay", 9, "The time (in ticks) a player has to be standing still before they are considered to be stopped", "This setting has no effect if toggle-on-move is set to false", "The value must be a positive whole number"),
TOGGLE_ON_COMBAT("toggle-on-combat", false, "If true, particles will be completely disabled while the player is in combat"),
TOGGLE_ON_COMBAT_DELAY("toggle-on-combat-delay", 15, "The time (in seconds) a player has to not be damaged/attacked to be considered out of combat", "This setting has no effect if toggle-on-combat is set to false", "The value must be a positive whole number"),
DISABLED_WORLDS("disabled-worlds", Collections.singletonList("disabled_world_name"), "A list of worlds that the plugin is disabled in"),
MAX_PARTICLES("max-particles", 3, "The maximum number of particles a player can apply at once", "The GUI will only display up to 21, don't set this any higher than that"),
MAX_GROUPS("max-groups", 10, "The maximum number of groups a player can have saved", "The GUI will only display up to 21, don't set this any higher than that"),

View file

@ -26,7 +26,6 @@ import java.util.stream.Collectors;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class ParticleGroupPresetManager extends Manager {

View file

@ -160,6 +160,9 @@ public class ParticleManager extends Manager implements Listener, Runnable {
*/
private void displayParticles(PPlayer pplayer, ParticlePair particle, Location location) {
if (!this.playerParticles.getManager(ParticleStyleManager.class).isEventHandled(particle.getStyle())) {
if (Setting.TOGGLE_ON_COMBAT.getBoolean() && pplayer.isInCombat())
return;
if (Setting.TOGGLE_ON_MOVE.getBoolean() && particle.getStyle().canToggleWithMovement() && pplayer.isMoving()) {
for (PParticle pparticle : DefaultStyles.FEET.getParticles(particle, location))
ParticleEffect.display(particle, pparticle, false, pplayer.getPlayer());

View file

@ -38,6 +38,11 @@ public class PPlayer {
*/
private boolean isMoving;
/**
* If the player is in combat
*/
private boolean inCombat;
/**
* Constructs a new PPlayer
*
@ -53,6 +58,7 @@ public class PPlayer {
this.particlesHidden = particlesHidden;
this.isMoving = false;
this.inCombat = false;
}
/**
@ -125,6 +131,24 @@ public class PPlayer {
return this.isMoving;
}
/**
* Sets the player's combat state
*
* @param inCombat True if the player is in combat, otherwise false
*/
public void setInCombat(boolean inCombat) {
this.inCombat = inCombat;
}
/**
* Gets if a player is in combat
*
* @return True if the player is in combat
*/
public boolean isInCombat() {
return this.inCombat;
}
/**
* Gets a ParticleGroup this player has by its name
*

View file

@ -154,7 +154,7 @@ public enum ParticleEffect {
return;
if (reloadConfig)
this.config.reloadConfig();
this.setDefaultSettings();
this.effectName = this.config.getString("effect-name");
this.enabled = this.config.getBoolean("enabled");

View file

@ -4,7 +4,6 @@ import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.PermissionManager;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.Player;
public class ParticleGroup {

View file

@ -1,7 +1,6 @@
package dev.esophose.playerparticles.particles;
import org.bukkit.Material;
import org.bukkit.entity.Player;
public class ParticleGroupPreset {

View file

@ -0,0 +1,95 @@
package dev.esophose.playerparticles.particles.listener;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.ConfigurationManager.Setting;
import dev.esophose.playerparticles.manager.DataManager;
import dev.esophose.playerparticles.particles.PPlayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
public class PPlayerCombatListener implements Listener {
private static final int CHECK_INTERVAL = 20;
private Map<UUID, Integer> timeSinceCombat = new HashMap<>();
public PPlayerCombatListener() {
DataManager dataManager = PlayerParticles.getInstance().getManager(DataManager.class);
Bukkit.getScheduler().runTaskTimer(PlayerParticles.getInstance(), () -> {
if (!Setting.TOGGLE_ON_COMBAT.getBoolean())
return;
List<UUID> toRemove = new ArrayList<>();
for (UUID uuid : this.timeSinceCombat.keySet()) {
PPlayer pplayer = dataManager.getPPlayer(uuid);
if (pplayer == null) {
toRemove.add(uuid);
} else {
int idleTime = this.timeSinceCombat.get(uuid);
pplayer.setInCombat(idleTime < Setting.TOGGLE_ON_COMBAT_DELAY.getInt());
if (pplayer.isInCombat())
this.timeSinceCombat.replace(uuid, idleTime + 1);
}
}
for (UUID uuid : toRemove)
this.timeSinceCombat.remove(uuid);
}, 0, CHECK_INTERVAL);
}
/**
* Used to detect if the player is moving
*
* @param event The event
*/
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerAttack(EntityDamageByEntityEvent event) {
if (!Setting.TOGGLE_ON_COMBAT.getBoolean())
return;
if (!(event.getEntity() instanceof Player))
return;
Player attacker;
if (event.getDamager() instanceof Projectile) {
Projectile projectile = (Projectile) event.getDamager();
if (!(projectile.getShooter() instanceof Player))
return;
attacker = (Player) projectile.getShooter();
} else if (event.getDamager() instanceof Player) {
attacker = (Player) event.getDamager();
} else return;
Player damaged = (Player) event.getEntity();
this.markInCombat(attacker);
this.markInCombat(damaged);
}
/**
* Marks the player as in combat
*
* @param player The player to mark
*/
private void markInCombat(Player player) {
UUID playerUUID = player.getUniqueId();
if (!this.timeSinceCombat.containsKey(playerUUID)) {
this.timeSinceCombat.put(playerUUID, 0);
} else {
this.timeSinceCombat.replace(playerUUID, 0);
}
}
}

View file

@ -1,14 +1,16 @@
package dev.esophose.playerparticles.particles;
package dev.esophose.playerparticles.particles.listener;
import dev.esophose.playerparticles.PlayerParticles;
import dev.esophose.playerparticles.manager.ConfigurationManager.Setting;
import dev.esophose.playerparticles.manager.DataManager;
import dev.esophose.playerparticles.particles.PPlayer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -35,7 +37,7 @@ public class PPlayerMovementListener implements Listener {
} else {
int standingTime = this.timeSinceLastMovement.get(uuid);
pplayer.setMoving(standingTime < Setting.TOGGLE_ON_MOVE_DELAY.getInt());
if (standingTime < Setting.TOGGLE_ON_MOVE_DELAY.getInt())
if (pplayer.isMoving())
this.timeSinceLastMovement.replace(uuid, standingTime + CHECK_INTERVAL);
}
}
@ -55,9 +57,11 @@ public class PPlayerMovementListener implements Listener {
if (!Setting.TOGGLE_ON_MOVE.getBoolean())
return;
if (event.getTo() != null && event.getTo().getBlock() == event.getFrom().getBlock())
Location to = event.getTo();
Location from = event.getFrom();
if (to == null || (to.getBlockX() == from.getBlockX() && to.getBlockY() == from.getBlockY() && to.getBlockZ() == from.getBlockZ()))
return;
UUID playerUUID = event.getPlayer().getUniqueId();
if (!this.timeSinceLastMovement.containsKey(playerUUID)) {
this.timeSinceLastMovement.put(playerUUID, 0);

View file

@ -57,7 +57,7 @@ public abstract class DefaultParticleStyle implements ParticleStyle {
*/
public final void loadSettings(boolean reloadConfig) {
if (reloadConfig)
this.config.reloadConfig();
this.setDefaultSettings();
this.styleName = this.config.getString("style-name");
this.enabled = this.config.getBoolean("enabled");