1 |
torben |
1671 |
package dk.thoerup.bukkit.hoeruputils; |
2 |
|
|
|
3 |
|
|
|
4 |
|
|
import org.bukkit.plugin.Plugin; |
5 |
|
|
import org.bukkit.Server; |
6 |
|
|
import org.bukkit.ChatColor; |
7 |
|
|
import org.bukkit.command.Command; |
8 |
|
|
import org.bukkit.command.CommandExecutor; |
9 |
|
|
import org.bukkit.command.CommandSender; |
10 |
|
|
import org.bukkit.entity.Player; |
11 |
|
|
|
12 |
|
|
import org.bukkit.event.player.PlayerListener; |
13 |
|
|
import org.bukkit.event.player.PlayerQuitEvent; |
14 |
|
|
|
15 |
|
|
import org.bukkit.inventory.Inventory; |
16 |
|
|
import org.bukkit.inventory.ItemStack; |
17 |
|
|
|
18 |
|
|
import org.bukkit.scheduler.BukkitScheduler; |
19 |
|
|
|
20 |
|
|
import java.util.HashMap; |
21 |
|
|
|
22 |
|
|
public class DiverCommand implements CommandExecutor { |
23 |
|
|
|
24 |
|
|
final static int LEATHER_COUNT = 3; |
25 |
|
|
final static int LEATHER_ID = 334; |
26 |
|
|
|
27 |
|
|
final static int TICK_COUNT = 20; |
28 |
|
|
|
29 |
|
|
HashMap<String,Integer> divers = new HashMap<String,Integer>(); |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
Plugin plugin; |
33 |
|
|
|
34 |
|
|
BukkitScheduler scheduler; |
35 |
|
|
public DiverCommand(Plugin plugin) { |
36 |
|
|
this.plugin = plugin; |
37 |
|
|
scheduler = plugin.getServer().getScheduler(); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
@Override |
41 |
|
|
public boolean onCommand(final CommandSender sender, Command command, String label, String[] args) { |
42 |
|
|
|
43 |
|
|
if (!(sender instanceof Player)) { |
44 |
|
|
return false; |
45 |
|
|
} |
46 |
|
|
final Player player = (Player) sender; |
47 |
|
|
|
48 |
|
|
if (divers.containsKey( sender.getName() )) { |
49 |
|
|
//disableDiver( player.getName() ); |
50 |
|
|
//player.sendMessage(ChatColor.GREEN + "Diver disabled - you're now gasping for air");; |
51 |
|
|
player.sendMessage(ChatColor.GREEN + "Diver: - you already have a airbladder"); |
52 |
|
|
} else { |
53 |
|
|
Inventory inv = player.getInventory(); |
54 |
|
|
if (inv.contains(LEATHER_ID, LEATHER_COUNT) ) { |
55 |
|
|
RefillAir r = new RefillAir(player); |
56 |
|
|
int taskid = scheduler.scheduleSyncRepeatingTask(plugin, r, (5*20), (5*20) ); |
57 |
|
|
divers.put(player.getName(), taskid); |
58 |
|
|
player.sendMessage(ChatColor.GREEN + "Diver: you created a airbladder from " + LEATHER_COUNT + " leather - go for a swim"); |
59 |
|
|
|
60 |
|
|
inv.removeItem( new ItemStack(LEATHER_ID, LEATHER_COUNT) ); |
61 |
|
|
} else { |
62 |
|
|
player.sendMessage(ChatColor.GREEN + "Diver: you need " + LEATHER_COUNT + " leather to create a airbladder"); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
return true; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
public PlayerListener getQuitHandler() { |
71 |
|
|
return quitHandler; |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
public void disableDiver(String name) { |
75 |
|
|
int taskid = divers.get( name ); |
76 |
|
|
scheduler.cancelTask( taskid ); |
77 |
|
|
divers.remove( name ); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
class RefillAir implements Runnable { |
81 |
|
|
|
82 |
|
|
int ticks = TICK_COUNT; |
83 |
|
|
|
84 |
|
|
Player player; |
85 |
|
|
public RefillAir(Player player) { |
86 |
|
|
this.player = player; |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
public void run() { |
90 |
|
|
int maxair = player.getMaximumAir(); |
91 |
|
|
if (player.getRemainingAir() < (maxair/2)) { |
92 |
|
|
player.setRemainingAir( maxair); |
93 |
|
|
|
94 |
|
|
ticks --; |
95 |
|
|
if (ticks == 1) { |
96 |
|
|
player.sendMessage(ChatColor.GREEN + "Diver: there's not much air left in the air-bladder"); |
97 |
|
|
} |
98 |
|
|
if (ticks <= 0) { |
99 |
|
|
disableDiver( player.getName() ); |
100 |
|
|
player.sendMessage(ChatColor.GREEN + "Diver: your air-bladder are empty!"); |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
PlayerListener quitHandler = new PlayerListener() { |
108 |
|
|
@Override |
109 |
|
|
public void onPlayerQuit(PlayerQuitEvent e) { |
110 |
|
|
Player p = e.getPlayer(); |
111 |
|
|
|
112 |
|
|
if (divers.containsKey( p.getName() )) { |
113 |
|
|
disableDiver( p.getName() ); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
} |
117 |
|
|
}; |
118 |
|
|
} |