From 21bb3a03f90876694b73b0dc64ea5313c94054ca Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Mon, 14 Nov 2016 21:32:38 -0500 Subject: [PATCH] Created a Player Proximity Task --- plugin.yml | 4 +- .../flairdoor/FlairDoorComponent.java | 16 +++++- .../flairdoor/commands/FlairMe.java | 8 ++- .../commands/SetProximityLocation.java | 49 +++++++++++++++++ .../listeners/PlayerProximityTask.java | 54 +++++++++++++++++++ .../PlayerProximityTaskLauncher.java | 12 +++++ .../flairdoor/listeners/PortalListener.java | 15 +++--- 7 files changed, 146 insertions(+), 12 deletions(-) create mode 100644 src/alisolarflare/components/flairdoor/commands/SetProximityLocation.java create mode 100644 src/alisolarflare/components/flairdoor/listeners/PlayerProximityTask.java create mode 100644 src/alisolarflare/components/flairdoor/listeners/PlayerProximityTaskLauncher.java diff --git a/plugin.yml b/plugin.yml index 1bbfbb3..fd3ecae 100644 --- a/plugin.yml +++ b/plugin.yml @@ -56,4 +56,6 @@ commands: starttimer: description: starts an ultrahardcore timer powerall: - description: activate every player's power \ No newline at end of file + description: activate every player's power + setproximitylocation: + description: sets one of two proximity blocks to create a space that players can change their flairs with using flairportals. Ask ali XD \ No newline at end of file diff --git a/src/alisolarflare/components/flairdoor/FlairDoorComponent.java b/src/alisolarflare/components/flairdoor/FlairDoorComponent.java index 34a9ead..7329694 100644 --- a/src/alisolarflare/components/flairdoor/FlairDoorComponent.java +++ b/src/alisolarflare/components/flairdoor/FlairDoorComponent.java @@ -1,15 +1,27 @@ package alisolarflare.components.flairdoor; +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Location; +import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import alisolarflare.components.Component; import alisolarflare.components.flairdoor.commands.FlairMe; +import alisolarflare.components.flairdoor.commands.SetProximityLocation; +import alisolarflare.components.flairdoor.listeners.PlayerProximityTaskLauncher; import alisolarflare.components.flairdoor.listeners.PortalListener; public class FlairDoorComponent extends Component { + public List playersToBeFlaired = new ArrayList(); + public Location startLocation = null; + public Location endLocation = null; @Override public void register(JavaPlugin plugin) { - registerCommand(plugin, new FlairMe()); - registerListener(plugin, new PortalListener(plugin)); + registerCommand(plugin, new FlairMe(this)); + registerCommand(plugin, new SetProximityLocation(this)); + registerListener(plugin, new PortalListener(plugin, this)); + registerListener(plugin, new PlayerProximityTaskLauncher(plugin, this)); } } diff --git a/src/alisolarflare/components/flairdoor/commands/FlairMe.java b/src/alisolarflare/components/flairdoor/commands/FlairMe.java index 8654260..d854304 100644 --- a/src/alisolarflare/components/flairdoor/commands/FlairMe.java +++ b/src/alisolarflare/components/flairdoor/commands/FlairMe.java @@ -4,13 +4,17 @@ import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import alisolarflare.components.PlayerCommand; -import alisolarflare.components.flairdoor.listeners.PortalListener; +import alisolarflare.components.flairdoor.FlairDoorComponent; public class FlairMe extends PlayerCommand { + private FlairDoorComponent component; + public FlairMe(FlairDoorComponent flairDoorComponent) { + this.component = flairDoorComponent; + } @Override public boolean OnCommand(CommandSender sender, String label, String[] args) { - PortalListener.playersToBeFlaired.add((Player) sender); + component.playersToBeFlaired.add((Player) sender); sender.sendMessage("Setup Successful! Walk through a portal to get your flair"); return true; } diff --git a/src/alisolarflare/components/flairdoor/commands/SetProximityLocation.java b/src/alisolarflare/components/flairdoor/commands/SetProximityLocation.java new file mode 100644 index 0000000..db57a00 --- /dev/null +++ b/src/alisolarflare/components/flairdoor/commands/SetProximityLocation.java @@ -0,0 +1,49 @@ +package alisolarflare.components.flairdoor.commands; + +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import alisolarflare.components.ModCommand; +import alisolarflare.components.flairdoor.FlairDoorComponent; + +public class SetProximityLocation extends ModCommand{ + + private FlairDoorComponent component; + + public SetProximityLocation(FlairDoorComponent flairDoorComponent) { + this.component = flairDoorComponent; + } + + @Override + public boolean OnCommand(CommandSender sender, String alias, String[] args) { + if (args.length < 1){ + sender.sendMessage("You must enter an argument"); + return false; + } + + Player player = (Player) sender; + String firstChar = args[0].substring(0, 1); + + switch(firstChar){ + case "s": + case "0": + component.startLocation = player.getLocation(); + return true; + case "e": + case "1": + component.endLocation = player.getLocation(); + return true; + default: + player.sendMessage("You must provide a vaild argument!"); + return false; + } + } + public String[] GetHelpText(String alias){ + return new String[] { + "Usage: /SetProximityLocation ", + "Use this command to set a proximity space: all players", + "within this space will become flair-able, and portals will", + "change their colour state." + }; + } +} diff --git a/src/alisolarflare/components/flairdoor/listeners/PlayerProximityTask.java b/src/alisolarflare/components/flairdoor/listeners/PlayerProximityTask.java new file mode 100644 index 0000000..b4d9ffb --- /dev/null +++ b/src/alisolarflare/components/flairdoor/listeners/PlayerProximityTask.java @@ -0,0 +1,54 @@ +package alisolarflare.components.flairdoor.listeners; + +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; + +import alisolarflare.components.flairdoor.FlairDoorComponent; + +public class PlayerProximityTask extends BukkitRunnable{ + private JavaPlugin plugin; + private FlairDoorComponent component; + private Location startLocation; + private Location endLocation; + + public PlayerProximityTask(JavaPlugin plugin, FlairDoorComponent component) { + this.plugin = plugin; + this.component = component; + this.startLocation = component.startLocation; + this.endLocation = component.endLocation; + } + + @Override + public void run() { + if (startLocation == null || endLocation == null){ + return; + } + int sX = startLocation.getBlockX(); + int sY = startLocation.getBlockY(); + int sZ = startLocation.getBlockZ(); + int eX = endLocation.getBlockX(); + int eY = endLocation.getBlockY(); + int eZ = endLocation.getBlockZ(); + int playerX; + int playerY; + int playerZ; + for (Player player : plugin.getServer().getOnlinePlayers()){ + playerX = player.getLocation().getBlockX(); + playerY = player.getLocation().getBlockY(); + playerZ = player.getLocation().getBlockZ(); + if(player.getLocation().getWorld() != startLocation.getWorld()) + continue; + if((playerX < sX && playerX < eX) ||(playerX > sX && playerX > eX)) + continue; + if((playerY < sY && playerY < eY) ||(playerY > sY && playerY > eY)) + continue; + if((playerZ < sZ && playerZ < eZ) ||(playerZ > sZ && playerZ > eZ)) + continue; + component.playersToBeFlaired.add(player); + + } + } + +} diff --git a/src/alisolarflare/components/flairdoor/listeners/PlayerProximityTaskLauncher.java b/src/alisolarflare/components/flairdoor/listeners/PlayerProximityTaskLauncher.java new file mode 100644 index 0000000..b7d7192 --- /dev/null +++ b/src/alisolarflare/components/flairdoor/listeners/PlayerProximityTaskLauncher.java @@ -0,0 +1,12 @@ +package alisolarflare.components.flairdoor.listeners; + +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; + +import alisolarflare.components.flairdoor.FlairDoorComponent; + +public class PlayerProximityTaskLauncher implements Listener{ + public PlayerProximityTaskLauncher(JavaPlugin plugin, FlairDoorComponent component){ + new PlayerProximityTask(plugin, component).runTaskTimer(plugin, 0, 20); + } +} diff --git a/src/alisolarflare/components/flairdoor/listeners/PortalListener.java b/src/alisolarflare/components/flairdoor/listeners/PortalListener.java index 2f42642..9d85a62 100644 --- a/src/alisolarflare/components/flairdoor/listeners/PortalListener.java +++ b/src/alisolarflare/components/flairdoor/listeners/PortalListener.java @@ -1,8 +1,5 @@ package alisolarflare.components.flairdoor.listeners; -import java.util.ArrayList; -import java.util.List; - import org.bukkit.Bukkit; import org.bukkit.DyeColor; import org.bukkit.Material; @@ -17,14 +14,18 @@ import org.bukkit.plugin.java.JavaPlugin; import com.earth2me.essentials.Essentials; import com.earth2me.essentials.User; +import alisolarflare.components.flairdoor.FlairDoorComponent; + public class PortalListener implements Listener{ - public static List playersToBeFlaired = new ArrayList(); public JavaPlugin plugin; Essentials essentials; + + private FlairDoorComponent component; - public PortalListener(JavaPlugin plugin) { + public PortalListener(JavaPlugin plugin, FlairDoorComponent component) { this.plugin = plugin; + this.component = component; this.essentials = ((Essentials) Bukkit.getPluginManager().getPlugin("Essentials")); } @EventHandler @@ -35,7 +36,7 @@ public class PortalListener implements Listener{ Player player = event.getPlayer(); - if(!(playersToBeFlaired.contains(player))){ + if(!(component.playersToBeFlaired.contains(player))){ return; } @@ -128,6 +129,6 @@ public class PortalListener implements Listener{ player.sendMessage("Adding the colour " + colourChanger + dyecolour.name() + "§f!"); player.sendMessage("Your name is now: " + user.getNickname() +"!"); - playersToBeFlaired.remove(player.getName()); + component.playersToBeFlaired.remove(player.getName()); } }