-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharena.js
More file actions
178 lines (154 loc) · 3.79 KB
/
Copy patharena.js
File metadata and controls
178 lines (154 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Todo:
// OSD display "Survive x more seconds"
// Beam out on failure
// Beam out on success
//
var utils = require('utils');
// Persistent storage
var store = persist('monsterArena', { spawnpos: ""});
// Configuration. Enemies to spawn
var enemies = [
org.bukkit.entity.EntityType.SPIDER,
org.bukkit.entity.EntityType.SKELETON,
org.bukkit.entity.EntityType.CREEPER,
org.bukkit.entity.EntityType.ZOMBIE,
];
// Global vars
var timer_id = 0;
var seq = 0;
var lifeTime = 0;
var spawnedMobs = [];
var engagedPlayers = [];
var spawnDelay = 10;
function spawnPos()
{
return utils.locationFromJSON(store.spawnpos);
}
// Helper functions
function spawn() {
if (seq > lifeTime) {
_ma_stop();
}
if ((seq % spawnDelay) === 0) {
// spawn something
var sp = spawnPos();
if(sp !== null)
{
sp.world.strikeLightningEffect(sp);
var mob_type = enemies[Math.floor(Math.random()*enemies.length)];
var mob = sp.world.spawnEntity(sp, mob_type);
spawnedMobs.push(mob);
}
}
if(seq % 30)
{
spawnDelay = Math.floor(spawnDelay/2)+1;
console.log("Spawn delay now: " + spawnDelay);
}
seq++;
}
function killAllMobs()
{
console.log("Killing all mobs");
for(var i = 0; i < spawnedMobs.length; i++)
{
var m = spawnedMobs[i];
if(!m.isValid())
continue;
m.remove();
}
spawnedMobs = [];
}
// Internal command functions
function _ma_join(player, ttl) {
if (timer_id !== 0) {
console.log("Starting spawner. Player: " + player);
// Already running
//clearInterval(timer_id);
lifeTime += ttl;
}
else {
console.log("Joining. Player: " + player);
timer_id = setInterval(spawn, 1000);
seq = 0;
killAllMobs();
lifeTime = ttl;
}
engagedPlayers.push(player);
}
function _ma_stop() {
if (timer_id !== 0) {
console.log("Stopping spawner");
clearInterval(timer_id);
timer_id = 0;
killAllMobs();
engagedPlayers = [];
}
}
// Event handling
function onPlayerDeath(event)
{
console.log("PlayerDeath " + event.getEntity());
for(var i = engagedPlayers.length-1; i >= 0; i--)
{
var p = engagedPlayers[i];
console.log("Checking if " + p + " is " + event.getEntity());
console.log("Checking if " + p.getUniqueId() + " is " + event.getEntity().getUniqueId());
console.log("Checking if " + p.isValid());
if(p.getUniqueId() == event.getEntity().getUniqueId())
{
console.log("Removing");
engagedPlayers.splice(i,1);
}
}
if(engagedPlayers.length == 0)
{
console.log("Everyone is dead");
_ma_stop();
}
}
events.playerDeath(onPlayerDeath);
// Command functions
function ma_join(args, sender) {
var ttl = 120;
if(args.length < 1)
{
sender.sendMessage("Usage: ma_join <player> <seconds>");
return;
}
var player = utils.player(args[0]);
if(player == null)
{
sender.sendMessage("Usage: ma_join <player> <seconds>");
return;
}
var t = parseInt(args[1]);
if(t > 0)
{
ttl = t;
}
_ma_join(player, ttl);
sender.sendMessage(player.name + " joined monsterArena");
}
function ma_stop(args, sender) {
_ma_stop();
sender.sendMessage("monsterArena: stopped")
}
function ma_spawn(args, sender) {
store.spawnpos = utils.locationToJSON(sender.getLocation());
sender.sendMessage("monsterArena: spawn point set");
}
function ma_info(args, sender) {
sender.sendMessage("monsterArena: spawn point: " + spawnPos());
if (timer_id === 0) {
sender.sendMessage("monsterArena: not running");
} else {
sender.sendMessage("monsterArena: running for " + seq + "/" + lifeTime + " secs");
sender.sendMessage("monsterArena: spawn delay " + spawnDelay);
sender.sendMessage("monsterArena: spawned mobs so far: " + spawnedMobs.length);
}
}
command(ma_spawn);
command(ma_join);
command(ma_stop);
command(ma_info);