This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextrude.js
194 lines (173 loc) · 5.67 KB
/
extrude.js
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//Imports
var ExtrudeData = require("./extrude.data.js");
var ExtrudeMode = require("./extrude.mode.js");
var Material = org.bukkit.Material;
//Aliases
var PlayerInteractEvent = org.bukkit.event.player.PlayerInteractEvent;
var PlayerMoveEvent = org.bukkit.event.player.PlayerMoveEvent;
var Action = org.bukkit.event.block.Action;
/** Compare one value against an array of values
* @param {any} toCompare
* @param {Array<any>} against
* @param {Boolean} abs test with absolute === instead of ==
*/
function isAnyOf(toCompare, against, abs) {
for (var i = 0; i < against.length; i++) {
if (abs) {
if (toCompare === against[i]) {
return true;
}
} else {
if (toCompare == against[i]) {
return true;
}
}
}
return false;
}
/**A map of player name to their extrude data
* @type {Map<String, ExtrudeData}
*/
var playersExtrudeData = {};
/**Get or create extrude data for a player
* @param {String} playerName
* @returns {ExtrudeData}
*/
function playerGetOrCreateExtrudeData(playerName) {
var result = playersExtrudeData[playerName];
if (result) return result;
result = new ExtrudeData();
playersExtrudeData[playerName] = result;
return result;
}
//Actions we care about when listening to player interactions
var toolSwitchActions = [Action.RIGHT_CLICK_AIR, Action.RIGHT_CLICK_BLOCK];
var toolModeUseActions = [Action.LEFT_CLICK_BLOCK, Action.LEFT_CLICK_AIR];
/**Handles every player interact w/ block event
* @param {PlayerInteractEvent} evt
*/
function playerInteractEventListener(evt) {
//Get the player, and his extrude data
var player = evt.getPlayer();
var extrudeData = playerGetOrCreateExtrudeData(player.getName());
var block = evt.getClickedBlock(); //CAN BE NULL
var itemInHand = evt.getItem();
//If no item in hand, return
if (!itemInHand) return;
var itemMaterial = itemInHand.getType();
//If we're using the player's set tool, we can do extrude-y things!
if (itemMaterial === extrudeData.toolMaterial) {
//Cancel the interaction event so it doesn't break/replace block
evt.setCancelled(true);
var action = evt.getAction();
if (isAnyOf(action, toolSwitchActions)) {
//Switch mode of tool
extrudeData.mode.next();
//Tell the player about it
player.sendMessage(
"[Extrude] Mode is now " + extrudeData.mode.mode
);
} else if (isAnyOf(action, toolModeUseActions)) {
switch (extrudeData.mode.mode) {
case ExtrudeMode.ADDBLOCK:
//If we're not dealing with a block, return
if (!evt.hasBlock()) return;
if (extrudeData.addBlock(
block.getX(),
block.getY(),
block.getZ(),
block.getType()
)) {
var bxo = block.getX() % 2;
var byo = block.getY() % 2;
var bzo = block.getZ() % 2;
var doBlack = false;
if (bxo !== 0) doBlack = !doBlack;
if (byo !== 0) doBlack = !doBlack;
if (bzo !== 0) doBlack = !doBlack;
if (doBlack) { //X IS EVEN
block.setType(Material.BLACK_WOOL);
} else {
block.setType(Material.YELLOW_WOOL);
}
} else {
player.sendMessage(
"[Extrude] Cannot add block twice"
);
return;
}
break;
case ExtrudeMode.REMOVEBLOCK:
//If we're not dealing with a block, return
if (!evt.hasBlock()) return;
if (!extrudeData.removeBlock(
block.getX(),
block.getY(),
block.getZ(),
player
)) {
player.sendMessage(
"[Extrude] That block wasn't recorded, no need to remove"
);
return;
}
break;
case ExtrudeMode.CLEAR:
extrudeData.clearBlocks(player);
break;
case ExtrudeMode.AUTOCLEAR:
extrudeData.autoClear = !extrudeData.autoClear;
break;
case ExtrudeMode.AXIS:
extrudeData.nextAxis();
break;
case ExtrudeMode.EXTRUDE:
if (extrudeData.isExtruding) {
extrudeData.doExtrude(player);
extrudeData.setExtruding(false);
} else {
extrudeData.setExtruding(true);
var loc = player.getLocation();
extrudeData.setOrigin(
loc.getBlockX(),
loc.getBlockY(),
loc.getBlockZ()
);
player.sendMessage("[Extrude] Origin " + extrudeData.origin.X + ", " + extrudeData.origin.Y + ", " + extrudeData.origin.Z);
}
break;
}
player.sendMessage(
extrudeData.createLastActionMessage()
);
}
}
}
events.on(PlayerInteractEvent, playerInteractEventListener);
function playerMoveEventListener(evt) {
var player = evt.getPlayer();
var pname = player.getName();
/**@type {ExtrudeData} */
var extrudeData = playersExtrudeData[pname];
if (extrudeData && extrudeData.isExtruding) {
var oldSteps = extrudeData.axisSteps;
var loc = player.getLocation();
switch (extrudeData.axis) {
case ExtrudeData.AXIS.X:
extrudeData.axisSteps = loc.getBlockX() - extrudeData.origin.X;
break;
case ExtrudeData.AXIS.Y:
extrudeData.axisSteps = loc.getBlockY() - extrudeData.origin.Y;
break;
case ExtrudeData.AXIS.Z:
extrudeData.axisSteps = loc.getBlockZ() - extrudeData.origin.Z;
break;
}
if (oldSteps !== extrudeData.axisSteps) {
player.sendMessage(
"[Extrude] Now " + extrudeData.axisSteps + " in " + extrudeData.axis
);
}
}
}
events.on(PlayerMoveEvent, playerMoveEventListener);