Skip to content

Commit

Permalink
Removed static modifier from delayedPlayers and delay in main class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragonphase authored and Dragonphase committed Jul 13, 2013
1 parent f34be1c commit 881882d
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 88 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,20 @@
<minecraft.version>1.5.1</minecraft.version>
<minecraft_version>1_5_R2</minecraft_version>
</properties>

<build>
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<!-- <targetPath>/</targetPath> -->
<includes>
<include>**</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>

<scm>
<connection>scm:git:git://github.com/Dragonphase/Kits.git</connection>
Expand Down
49 changes: 37 additions & 12 deletions src/main/java/com/dragonphase/Kits/Commands/KitCommandExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,37 @@ public class KitCommandExecutor implements CommandExecutor{

public KitCommandExecutor(Kits instance) {
plugin = instance;
Kit.setParent(plugin);
}

@SuppressWarnings("deprecation")
@Override
public boolean onCommand(CommandSender sender, Command cmd, String command, String[] args) {
if (!(sender instanceof Player)) return false;
if (!(sender instanceof Player)){
if (args.length == 2){
String arg = args[0];

Player receiver = Bukkit.getPlayerExact(args[1]);

if (Kit.exists(arg)){
if (receiver != null){
ItemStack[] itemList = Kit.getKit(arg);
for (int i = 0; i < itemList.length; i ++){
receiver.getInventory().setItem(i, itemList[i]);
}
receiver.updateInventory();
receiver.sendMessage(Message.info("Kit " + arg + " spawned by Console."));
sender.sendMessage(Message.info("Kit " + arg + " spawned for " + receiver.getName() + "."));
}else{
sender.sendMessage(Message.warning(args[1] + " is not online."));
}
}else{
sender.sendMessage(Message.warning("Kit " + arg + " does not exist."));
}
}
return false;
}

Player player = (Player) sender;

if (args.length == 0){
Expand All @@ -40,11 +65,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String command, Stri
player.sendMessage(Message.info("/kit remove <kitname>"));
}else{
if (player.hasPermission("kits.spawn." + arg)){
if (Kits.playerDelayed(player)){
if (Kits.getRemainingTime(player) < 1){
Kits.removeDelayedPlayer(player);
if (plugin.playerDelayed(player)){
if (plugin.getRemainingTime(player) < 1){
plugin.removeDelayedPlayer(player);
}else{
int remaining = Kits.getRemainingTime(player);
int remaining = plugin.getRemainingTime(player);
String seconds = remaining == 1 ? " second" : " seconds";
player.sendMessage(Message.warning("You must wait " + remaining + seconds + " before spawning another kit."));
return false;
Expand All @@ -58,7 +83,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String command, Stri
player.updateInventory();
player.sendMessage(Message.info("Kit " + arg + " spawned."));

if (!player.hasPermission("kits.bypassdelay") && Kits.getDelay(1) > 0) Kits.addDelayedPlayer(player);
if (!player.hasPermission("kits.bypassdelay") && plugin.getDelay(1) > 0) plugin.addDelayedPlayer(player);
}else{
player.sendMessage(Message.warning("Kit " + arg + " does not exist."));
}
Expand Down Expand Up @@ -94,7 +119,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String command, Stri
}else if (arg.equalsIgnoreCase("remove")){
if (player.hasPermission("kits.admin")){
if (Kit.exists(args[1])){
Kits.kitsFile.set(args[1], null, false);
plugin.getKitsConfig().set(args[1], null, false);
player.sendMessage(Message.info("Kit " + args[1] + " has been removed."));
}else{
player.sendMessage(Message.warning("Kit " + args[1] + " does not exist."));
Expand All @@ -106,11 +131,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String command, Stri
if (player.hasPermission("kits.others.spawn." + arg)){
Player receiver = Bukkit.getPlayerExact(args[1]);

if (Kits.playerDelayed(receiver)){
if (Kits.getRemainingTime(receiver) < 1){
Kits.removeDelayedPlayer(receiver);
if (plugin.playerDelayed(receiver)){
if (plugin.getRemainingTime(receiver) < 1){
plugin.removeDelayedPlayer(receiver);
}else{
int remaining = Kits.getRemainingTime(receiver);
int remaining = plugin.getRemainingTime(receiver);
String seconds = remaining == 1 ? " second" : " seconds";
player.sendMessage(Message.warning(receiver.getName() + " must wait " + remaining + seconds + " before spawning another kit."));
return false;
Expand All @@ -127,7 +152,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String command, Stri
receiver.sendMessage(Message.info("Kit " + arg + " spawned by " + player.getName() + "."));
player.sendMessage(Message.info("Kit " + arg + " spawned for " + receiver.getName() + "."));

if (!receiver.hasPermission("kits.bypassdelay") && Kits.getDelay(1) > 0) Kits.addDelayedPlayer(receiver);
if (!receiver.hasPermission("kits.bypassdelay") && plugin.getDelay(1) > 0) plugin.addDelayedPlayer(receiver);
}else{
player.sendMessage(Message.warning(args[1] + " is not online."));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String command, Stri
}
}else{
String kits = "";
for (String string : Kits.kitsFile.getKeys(false)){
for (String string : plugin.getKitsConfig().getKeys(false)){
kits += string + ", ";
}
if (kits != "" || !kits.isEmpty()){
Expand Down
40 changes: 23 additions & 17 deletions src/main/java/com/dragonphase/Kits/Kits.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@

public class Kits extends JavaPlugin{
public final Logger logger = Logger.getLogger("Minecraft");
public static Kits plugin;
public static FileManager configurationFile;
public static FileManager kitsFile;
private FileManager config, kits;

private static HashMap<Player, Long> delayedPlayers;
private static int delay;
private HashMap<Player, Long> delayedPlayers;
private int delay;

@Override
public void onDisable(){
Expand All @@ -34,11 +32,11 @@ public void onEnable(){
saveDefaultConfig();
getConfig().options().copyDefaults(true);

configurationFile = new FileManager(this, "config.yml");
kitsFile = new FileManager(this, "kits.yml");
config = new FileManager(this, "config.yml");
setKits(new FileManager(this, "kits.yml"));

delayedPlayers = new HashMap<Player, Long>();
delay = Kits.configurationFile.getInt("options.delay");
delay = config.getInt("options.delay");

getServer().getPluginManager().registerEvents(new EventListener(this), this);

Expand All @@ -48,10 +46,10 @@ public void onEnable(){

public void reload(){
reloadConfig();
configurationFile.loadFile();
kitsFile.loadFile();
config.loadFile();
getKitsConfig().loadFile();

delay = Kits.configurationFile.getInt("options.delay");
delay = config.getInt("options.delay");
}

public String getPluginDetails(){
Expand All @@ -66,27 +64,35 @@ public String getPluginVersion(){
return getDescription().getVersion();
}

public static void addDelayedPlayer(Player player){
public void addDelayedPlayer(Player player){
delayedPlayers.put(player, System.currentTimeMillis());
}

public static void removeDelayedPlayer(Player player){
public void removeDelayedPlayer(Player player){
delayedPlayers.remove(player);
}

public static boolean playerDelayed(Player player){
public boolean playerDelayed(Player player){
return delayedPlayers.containsKey(player);
}

public static Long getPlayerDelay(Player player){
public Long getPlayerDelay(Player player){
return delayedPlayers.get(player);
}

public static int getDelay(int multiplier){
public int getDelay(int multiplier){
return delay*multiplier;
}

public static int getRemainingTime(Player player){
public int getRemainingTime(Player player){
return (int) (getDelay(1) - ((System.currentTimeMillis() - getPlayerDelay(player))/1000));
}

public FileManager getKitsConfig() {
return kits;
}

public void setKits(FileManager kits) {
this.kits = kits;
}
}
82 changes: 47 additions & 35 deletions src/main/java/com/dragonphase/Kits/Listeners/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.logging.Logger;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
Expand Down Expand Up @@ -43,22 +44,22 @@ public void onInventoryClose(InventoryCloseEvent event){
if (((MetadataValue)player.getMetadata("editingKit").get(0)).asBoolean()){
String kit = inventory.getTitle();

Kits.kitsFile.set(kit, inventory.getContents(), false);
plugin.getKitsConfig().set(kit, inventory.getContents(), false);

player.sendMessage(Message.info("Kit " + kit + " has been updated."));
player.setMetadata("editingKit", new FixedMetadataValue(plugin, Boolean.valueOf(false)));

Kits.kitsFile.loadFile();
plugin.getKitsConfig().loadFile();
}
if (((MetadataValue)player.getMetadata("creatingKit").get(0)).asBoolean()){
String kit = inventory.getTitle();

Kits.kitsFile.set(kit, inventory.getContents(), false);
plugin.getKitsConfig().set(kit, inventory.getContents(), false);

player.sendMessage(Message.info("Kit " + kit + " has been created."));
player.setMetadata("creatingKit", new FixedMetadataValue(plugin, Boolean.valueOf(false)));

Kits.kitsFile.loadFile();
plugin.getKitsConfig().loadFile();
}
}
catch (Exception ex)
Expand All @@ -67,47 +68,58 @@ public void onInventoryClose(InventoryCloseEvent event){
}
}

@SuppressWarnings("deprecation")
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event)
{
if ((event.getAction() != Action.RIGHT_CLICK_BLOCK) && (event.getAction() != Action.RIGHT_CLICK_AIR)) return;
if (event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getState() instanceof Sign){
handleSignClick(event.getPlayer(), (Sign)event.getClickedBlock().getState());

if ((event.getClickedBlock().getState() instanceof Sign)){
for (int i = 0; i < 4; i++){
if (((Sign)event.getClickedBlock().getState()).getLines()[i].equalsIgnoreCase("[kit]") && i != 3){
try{
Player player = event.getPlayer();
String arg = ((Sign)event.getClickedBlock().getState()).getLines()[i+1];
if (player.hasPermission("kits.spawn." + arg)){
if (Kits.playerDelayed(player)){
if (Kits.getRemainingTime(player) < 1){
Kits.removeDelayedPlayer(player);
}else{
int remaining = Kits.getRemainingTime(player);
String seconds = remaining == 1 ? " second" : " seconds";
player.sendMessage(Message.warning("You must wait " + remaining + seconds + " before spawning another kit."));
return;
}
}
if (Kit.exists(arg)){
ItemStack[] itemList = Kit.getKit(arg);
for (int x = 0; x < itemList.length; x++){
player.getInventory().setItem(x, itemList[x]);
}
player.updateInventory();
player.sendMessage(Message.info("Kit " + arg + " spawned."));
event.setUseItemInHand(Result.DENY);
event.setUseInteractedBlock(Result.DENY);
}else if (event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getTargetBlock(null, 5).getState() instanceof Sign){
handleSignClick(event.getPlayer(), (Sign)event.getPlayer().getTargetBlock(null, 5).getState());

if ((!player.hasPermission("kits.bypassdelay")) && (Kits.getDelay(1) > 0)) Kits.addDelayedPlayer(player);
event.setUseItemInHand(Result.DENY);
event.setUseInteractedBlock(Result.DENY);
}
}

@SuppressWarnings("deprecation")
public void handleSignClick(Player player, Sign sign){
for (int i = 0; i < 4; i++){
if (sign.getLines()[i].equalsIgnoreCase("[kit]") && i != 3){
try{
String arg = sign.getLines()[i+1];
if (player.hasPermission("kits.spawn." + arg)){
if (plugin.playerDelayed(player)){
if (plugin.getRemainingTime(player) < 1){
plugin.removeDelayedPlayer(player);
}else{
int remaining = plugin.getRemainingTime(player);
String seconds = remaining == 1 ? " second" : " seconds";
player.sendMessage(Message.warning("You must wait " + remaining + seconds + " before spawning another kit."));
return;
}
else{ player.sendMessage(Message.warning("Kit " + arg + " does not exist.")); }
}
if (Kit.exists(arg)){
ItemStack[] itemList = Kit.getKit(arg);
for (int x = 0; x < itemList.length; x++){
player.getInventory().setItem(x, itemList[x]);
}
player.updateInventory();
player.sendMessage(Message.info("Kit " + arg + " spawned."));

if ((!player.hasPermission("kits.bypassdelay")) && (plugin.getDelay(1) > 0)) plugin.addDelayedPlayer(player);
}
else{
player.sendMessage(Message.warning("Incorrect Permissions."));
player.sendMessage(Message.warning("Kit " + arg + " does not exist."));
}
}catch (Exception ex){
continue;
}
else{
player.sendMessage(Message.warning("Incorrect Permissions."));
}
}catch (Exception ex){
continue;
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/dragonphase/Kits/Util/Kit.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,22 @@

public class Kit {

private static Kits plugin;

public static void setParent(Kits instance){
plugin = instance;
}

public static boolean exists(String kitName){
boolean exists = false;
for (String key : Kits.kitsFile.getKeys(false)){
for (String key : plugin.getKitsConfig().getKeys(false)){
if (key.equals(kitName)) exists = true;
}
return exists;
}

public static ItemStack[] getKit(String kitName){
return Kits.kitsFile.getInventory(kitName);
return plugin.getKitsConfig().getInventory(kitName);
}

public static void create(Plugin plugin, Player player, String kitName) {
Expand All @@ -34,6 +40,6 @@ public static void edit(Plugin plugin, Player player, String kitName) {
}

public static int kitSize(String kit){
return Kits.kitsFile.getConfigurationSection(kit).getKeys(false).size();
return plugin.getKitsConfig().getConfigurationSection(kit).getKeys(false).size();
}
}
Loading

0 comments on commit 881882d

Please sign in to comment.