-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventAPI.js
More file actions
275 lines (269 loc) · 9.16 KB
/
eventAPI.js
File metadata and controls
275 lines (269 loc) · 9.16 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/**
*
* Description: This code is some comprehension of what I learned in custom nps scripting.
* When I created maps, I dreamed of adding my own event system.
* Several years have passed since then, during which I studied custom NPCs, modding, left minecraft and tried different 'zones of life'.
* Now I was able to write a similar script that implements some of the features and can be useful.
* For me, this is some point, the ability to leave scripting with the knowledge that my script is used in maps and helps,
* albeit rarely, to optimize the development of the game logic.
* I would be grateful if you will did not edit the comments when using it, it will help my git hub and my projects develop
*
* Author: Evanechecssss
* Link: https://bio.link/evanechecssss
* GitHub: https://github.com/Evanechecssss/cnps_event_pattern
* Tutorial site: https://evanechecssss.github.io/cnps_event_pattern
* Date: 02.11.2021
*/
/**
* String constants
*/
var strEventList = "\n===CUSTOM EVENT LIST===\n"
var strEventEvent = "EVENT: "
var strEventDescription = "DESCRIPTION: "
var strEventConstructor = "DESCRIPTION: "
var fileTutorialName = "tutor.html"
var fileEventsName = "custom_events.json"
var eventDescription = "description"
var eventArgs = "args"
var eventName = "name"
var exceptionTutorial = "\n\nYou lose tutoryal file\n\n"
var exceptionExist = "\n\n %s - event is not exist\n\n"
var exceptionCorrect = "\n\n %s- is not correct! You args most be as %s \n\n"
var exceptionCorrect = "\n\nYou has uncorrect type of entity= %S \n\n"
var exceptionEmpty = "\n\nYour events file is empty\n\n"
var exceptionTileEntity = "\n\nBlock hasn't tile entity= %s \n\n"
/**
* Helper classes
*/
var DIR = Java.type("noppes.npcs.api.NpcAPI").Instance().getWorldDir().toPath().getParent().resolve("custom_events") // DIR is place, where you should keep you custom events
var JFILE = Java.type("java.io.File") //For work with files
var JNIOFile = Java.type("java.nio.file.Files") //For work with files
var DESKTOP = Java.type('java.awt.Desktop') //For work with browsers
var NPCException = Java.type("noppes.npcs.api.CustomNPCsException") //For call exceptions
var MCPlayer = Java.type("net.minecraft.entity.player.EntityPlayer") //Is main Player calss
var MCNPC = Java.type("noppes.npcs.entity.EntityNPCInterface") //Is main npc class
var PLAYER_DATA = Java.type("noppes.npcs.controllers.data.PlayerData") //Is class of player data
var TILE_SCRIPTED = Java.type("noppes.npcs.blocks.tiles.TileScripted") //Is main tile block class
var TILE_SCRIPTED_DOOR = Java.type("noppes.npcs.blocks.tiles.TileScriptedDoor") //Is scripteddoor tile class
var BLOCK_WRAPPER = Java.type("noppes.npcs.api.wrapper.BlockWrapper") //Is main block class
var JSTRING = Java.type("java.lang.String") //Is class for string formating
/**
* Methods for public usage
*/
/**
* If you will run it, event list will show in console!
* If you add some custom events in custom event file, they will be show in console
*/
function EVENT_LIST() {
var EVENTS = jsonObjectGet()
for (var key in EVENTS) {
print(strEventList)
print(strEventEvent + key)
print(strEventDescription + EVENTS[key][eventDescription])
print(strEventConstructor + objToJSON(EVENTS[key][eventArgs]))
}
}
/**
*
* @param name of event, you can to know it, if you call EVENT_LIST()
* @return default instance of this event
*/
function DEF(name) {
var EVENTS = jsonObjectGet()
if (!(name in EVENTS)) {
throw new NPCException()
}
return EVENTS[name]
}
/**
* YOU SHOULD USE IT, BECOUSE YOU CAN GET SAFE EVENT INSTANCE
* You can call TUTORIAL(), if you whant to know how use it
* @param name of event, you can to know it, if you call EVENT_LIST()
* @param args of event, they replace default values, names in object dosnt matter, only leght
* @return instance with you values
*/
function INSTANCE(name, args) {
var EVENTS = jsonObjectGet()
if (!(name in EVENTS)) {
throw new NPCException(JSTRING.format(exceptionExist, name))
}
EVENTS[name][eventArgs] = replaceNewData(EVENTS[name][eventArgs], args)
return EVENTS[name]
}
/**
*
* @param event , you can get it with INSTANCE(name, args)
*/
function SEND_TO_ALL_NPCS(event) {
sendToAllEntity(event)
}
/**
*
* @param event , you can get it with INSTANCE(name, args)
*/
function SEND_TO_ALL_PLAYERS(event) {
sendToAllPlayers(event)
}
/**
*
* @param event , you can get it with INSTANCE(name, args)
* @param players , array of players
*/
function SEND_TO_PLAYERS(event, players) {
sendToEntities(players, event)
}
/**
*
* @param event , you can get it with INSTANCE(name, args)
* @param npcs , array of npcs
*/
function SEND_TO_NPCS(event, npcs) {
sendToEntities(npcs, event)
}
/**
*
* @param event , you can get it with INSTANCE(name, args)
* @param blocks , array of blocks
*/
function SEND_TO_BLOCKS(event, blocks) {
sendToBlocks(event, blocks)
}
/**
*
* @param event , you can get it with INSTANCE(name, args)
* @param x , the x coordinate of the block
* @param y , the y coordinate of the block
* @param z , the z coordinate of the block
*/
function SEND_TO_XYZ_BLOCK(event, x, y, z) {
var block = Java.type("noppes.npcs.api.NpcAPI").Instance().getIWorld(0).getBlock(x, y, z)
privateSend(block, event)
}
/**
* Methods for only extened users
*/
/**
*
* Old data from json primitive will be raplaced with new values.
* The given from .Jeson primitive will be populated with the new object data. Data not specified in the new object will be the default data, the name of the fields of the 'value object' event is not important at all, the fields of the event of the primitive are important
*
* @param {Object} def - default object
* @param {Object} val - value object
* @returns default object with new values
*/
function replaceNewData(def, val) {
var returnedDef = def
if (Object.keys(returnedDef).length != Object.keys(val).length) {
throw new NPCException(JSTRING.format(exceptionCorrect, objToJSON(val), objToJSON(def)))
}
for (var i = 0; i < Object.keys(returnedDef).length; i++) {
returnedDef[Object.keys(returnedDef)[i]] = val[Object.keys(val)[i]]
}
return returnedDef
}
/**
*
* @returns json file with events as object
*/
function jsonObjectGet() {
var file
var reader
var line
var text = ""
var obj = null
file = DIR.resolve(fileEventsName)
if (JNIOFile.exists(file)) {
reader = JNIOFile.newBufferedReader(file)
while ((line = reader.readLine()) != null) {
text += line
}
reader.close()
try {
obj = JSON.parse(text)
} catch (err) {
throw new NPCException(exceptionEmpty)
}
}
return obj
}
/**
*
* @param {Object} object
* @returns JSON String from object
*/
function objToJSON(object) {
return JSON.stringify(object, null, 4)
}
/**
*
* @param {INSTANCE()} event
*/
function sendToAllEntity(event) {
var npcs = Java.type("noppes.npcs.api.NpcAPI").Instance().getIWorld(1).getAllEntities(2)
sendToEntities(npcs, event)
}
/**
*
* @param {INSTANCE()} event
*/
function sendToAllPlayers(event) {
var players = Java.type("noppes.npcs.api.NpcAPI").Instance().getIWorld(1).getAllPlayers()
sendToEntities(players, event)
}
/**
*
* @param {INSTANCE()} event
* @param {BLOCKS[]} blocks
*/
function sendToBlocks(event, blocks) {
for (var index = 0; index < blocks.length; index++) {
privateSend(blocks[index], event)
}
}
/**
*
* @param {ENTITIES[]} entities
* @param {INSTANCE()} event
*/
function sendToEntities(entities, event) {
for (var index = 0; index < entities.length; index++) {
privateSend(entities[index].getMCEntity(), event)
}
}
/**
* P.S: noppes level genius: name class with Interface suffix
* @param {CustomNPCInterface} npc
* @returns scripts from npc
*/
function getScriptsFromNPC(npc) {
return npc.script
}
function getScriptsFromPlayer(player) {
return PLAYER_DATA.get(player).scriptData
}
function getScriptsFromBlock(block) {
var mcTile = block.getMCTileEntity()
if (mcTile instanceof TILE_SCRIPTED || mcTile instanceof TILE_SCRIPTED_DOOR) {
return mcTile;
} else {
throw new NPCException(JSTRING.format(exceptionTileEntity, mcTile.toString()))
}
}
function privateSend(mcEntity, event) {
var data;
if (mcEntity instanceof MCPlayer) {
data = getScriptsFromPlayer(mcEntity)
} else if (mcEntity instanceof MCNPC) {
data = getScriptsFromNPC(mcEntity)
} else if (mcEntity instanceof BLOCK_WRAPPER) {
data = getScriptsFromBlock(mcEntity)
} else {
throw new NPCException(JSTRING.format(exceptionCorrect, mcEntity.toString()))
}
privateCallMethodsFromScripts(data.getScripts(), event)
}
function privateCallMethodsFromScripts(scripts, event) {
for (var i = 0; i < scripts.length; i++) {
scripts[i].run(event[eventName], event[eventArgs])
}
}