Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
8ab2b446
Commit
8ab2b446
authored
Apr 29, 2022
by
Melledy
Browse files
Clean script events
parent
30886da0
Changes
7
Show whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/game/dungeons/DungeonChallenge.java
View file @
8ab2b446
...
...
@@ -9,6 +9,7 @@ import emu.grasscutter.game.entity.EntityMonster;
import
emu.grasscutter.game.entity.GameEntity
;
import
emu.grasscutter.game.world.Scene
;
import
emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType
;
import
emu.grasscutter.scripts.constants.EventType
;
import
emu.grasscutter.scripts.data.SceneGroup
;
import
emu.grasscutter.scripts.data.SceneMonster
;
import
emu.grasscutter.server.packet.send.PacketDungeonChallengeBeginNotify
;
...
...
@@ -73,9 +74,9 @@ public class DungeonChallenge {
getScene
().
broadcastPacket
(
new
PacketDungeonChallengeFinishNotify
(
this
));
if
(
this
.
isSuccess
())
{
this
.
getScene
().
getScriptManager
().
onChallengeSuccess
(
);
this
.
getScene
().
getScriptManager
().
callEvent
(
EventType
.
EVENT_CHALLENGE_SUCCESS
,
null
);
}
else
{
this
.
getScene
().
getScriptManager
().
onChallengeFailure
(
);
this
.
getScene
().
getScriptManager
().
callEvent
(
EventType
.
EVENT_CHALLENGE_FAIL
,
null
);
}
}
...
...
src/main/java/emu/grasscutter/game/world/Scene.java
View file @
8ab2b446
...
...
@@ -20,9 +20,11 @@ import emu.grasscutter.net.packet.BasePacket;
import
emu.grasscutter.net.proto.AttackResultOuterClass.AttackResult
;
import
emu.grasscutter.net.proto.VisionTypeOuterClass.VisionType
;
import
emu.grasscutter.scripts.SceneScriptManager
;
import
emu.grasscutter.scripts.constants.EventType
;
import
emu.grasscutter.scripts.data.SceneBlock
;
import
emu.grasscutter.scripts.data.SceneGadget
;
import
emu.grasscutter.scripts.data.SceneGroup
;
import
emu.grasscutter.scripts.data.ScriptArgs
;
import
emu.grasscutter.server.packet.send.PacketEntityFightPropUpdateNotify
;
import
emu.grasscutter.server.packet.send.PacketLifeStateChangeNotify
;
import
emu.grasscutter.server.packet.send.PacketSceneEntityAppearNotify
;
...
...
@@ -505,7 +507,7 @@ public class Scene {
entity
.
setState
(
g
.
state
);
this
.
addEntity
(
entity
);
this
.
getScriptManager
().
onGadgetCreate
(
entity
);
this
.
getScriptManager
().
callEvent
(
EventType
.
EVENT_GADGET_CREATE
,
new
ScriptArgs
(
entity
.
getConfigId
())
);
}
}
}
...
...
src/main/java/emu/grasscutter/scripts/SceneScriptManager.java
View file @
8ab2b446
...
...
@@ -18,7 +18,7 @@ import emu.grasscutter.game.entity.EntityGadget;
import
emu.grasscutter.game.entity.EntityMonster
;
import
emu.grasscutter.game.entity.GameEntity
;
import
emu.grasscutter.game.world.Scene
;
import
emu.grasscutter.scripts.constants.
Script
EventType
;
import
emu.grasscutter.scripts.constants.EventType
;
import
emu.grasscutter.scripts.constants.ScriptGadgetState
;
import
emu.grasscutter.scripts.constants.ScriptRegionShape
;
import
emu.grasscutter.scripts.data.SceneBlock
;
...
...
@@ -29,6 +29,7 @@ import emu.grasscutter.scripts.data.SceneInitConfig;
import
emu.grasscutter.scripts.data.SceneMonster
;
import
emu.grasscutter.scripts.data.SceneSuite
;
import
emu.grasscutter.scripts.data.SceneTrigger
;
import
emu.grasscutter.scripts.data.ScriptArgs
;
import
it.unimi.dsi.fastutil.ints.Int2ObjectMap
;
import
it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap
;
...
...
@@ -120,7 +121,7 @@ public class SceneScriptManager {
bindings
=
ScriptLoader
.
getEngine
().
createBindings
();
// Set variables
bindings
.
put
(
"EventType"
,
new
Script
EventType
());
// TODO - make static class to avoid instantiating a new class every scene
bindings
.
put
(
"EventType"
,
new
EventType
());
// TODO - make static class to avoid instantiating a new class every scene
bindings
.
put
(
"GadgetState"
,
new
ScriptGadgetState
());
bindings
.
put
(
"RegionShape"
,
new
ScriptRegionShape
());
bindings
.
put
(
"ScriptLib"
,
getScriptLib
());
...
...
@@ -208,76 +209,30 @@ public class SceneScriptManager {
// Events
private
LuaValue
getFunc
(
String
name
)
{
return
(
LuaValue
)
this
.
getBindings
().
get
(
name
);
}
public
void
onGadgetCreate
(
EntityGadget
gadget
)
{
LuaTable
params
=
new
LuaTable
();
params
.
set
(
"param1"
,
gadget
.
getConfigId
());
for
(
SceneTrigger
trigger
:
this
.
getTriggersByEvent
(
ScriptEventType
.
EVENT_GADGET_CREATE
))
{
LuaValue
condition
=
getFunc
(
trigger
.
condition
);
LuaValue
ret
=
condition
.
call
(
this
.
getScriptLibLua
(),
params
);
if
(
ret
.
checkboolean
()
==
true
)
{
LuaValue
action
=
getFunc
(
trigger
.
action
);
action
.
call
(
this
.
getScriptLibLua
(),
LuaValue
.
NIL
);
}
}
}
public
void
onOptionSelect
(
int
entityId
,
int
optionId
)
{
GameEntity
entity
=
this
.
getScene
().
getEntityById
(
entityId
);
public
void
callEvent
(
int
eventType
,
ScriptArgs
params
)
{
for
(
SceneTrigger
trigger
:
this
.
getTriggersByEvent
(
eventType
))
{
LuaValue
condition
=
null
;
if
(
entity
==
null
||
!(
entity
instanceof
EntityGadget
))
{
return
;
if
(
trigger
.
condition
!=
null
&&
!
trigger
.
condition
.
isEmpty
(
))
{
condition
=
(
LuaValue
)
this
.
getBindings
().
get
(
trigger
.
condition
)
;
}
LuaTable
params
=
new
LuaTable
();
params
.
set
(
"param1"
,
entity
.
getConfigId
());
params
.
set
(
"param2"
,
optionId
);
for
(
SceneTrigger
trigger
:
this
.
getTriggersByEvent
(
ScriptEventType
.
EVENT_SELECT_OPTION
))
{
LuaValue
condition
=
getFunc
(
trigger
.
condition
);
LuaValue
ret
=
LuaValue
.
TRUE
;
LuaValue
ret
=
condition
.
call
(
this
.
getScriptLibLua
(),
params
);
if
(
condition
!=
null
)
{
LuaValue
args
=
LuaValue
.
NIL
;
if
(
ret
.
checkboolean
()
==
true
)
{
LuaValue
action
=
getFunc
(
trigger
.
action
);
action
.
call
(
this
.
getScriptLibLua
(),
LuaValue
.
NIL
);
}
if
(
params
!=
null
)
{
args
=
CoerceJavaToLua
.
coerce
(
params
);
}
}
public
void
onMonsterSpawn
(
EntityMonster
entity
)
{
LuaTable
params
=
new
LuaTable
();
params
.
set
(
"param1"
,
entity
.
getConfigId
());
for
(
SceneTrigger
trigger
:
this
.
getTriggersByEvent
(
ScriptEventType
.
EVENT_ANY_MONSTER_LIVE
))
{
LuaValue
condition
=
getFunc
(
trigger
.
condition
);
LuaValue
ret
=
condition
.
call
(
this
.
getScriptLibLua
(),
params
);
if
(
ret
.
checkboolean
()
==
true
)
{
LuaValue
action
=
getFunc
(
trigger
.
action
);
action
.
call
(
this
.
getScriptLibLua
(),
LuaValue
.
NIL
);
}
}
ret
=
condition
.
call
(
this
.
getScriptLibLua
(),
args
);
}
public
void
onChallengeSuccess
()
{
for
(
SceneTrigger
trigger
:
this
.
getTriggersByEvent
(
ScriptEventType
.
EVENT_CHALLENGE_SUCCESS
))
{
LuaValue
action
=
getFunc
(
trigger
.
action
);
if
(
ret
.
checkboolean
()
==
true
)
{
LuaValue
action
=
(
LuaValue
)
this
.
getBindings
().
get
(
trigger
.
action
);
action
.
call
(
this
.
getScriptLibLua
(),
LuaValue
.
NIL
);
}
}
public
void
onChallengeFailure
()
{
for
(
SceneTrigger
trigger
:
this
.
getTriggersByEvent
(
ScriptEventType
.
EVENT_CHALLENGE_FAIL
))
{
LuaValue
action
=
getFunc
(
trigger
.
action
);
action
.
call
(
this
.
getScriptLibLua
(),
LuaValue
.
NIL
);
}
}
}
src/main/java/emu/grasscutter/scripts/ScriptLib.java
View file @
8ab2b446
...
...
@@ -13,8 +13,10 @@ import emu.grasscutter.game.dungeons.DungeonChallenge;
import
emu.grasscutter.game.entity.EntityGadget
;
import
emu.grasscutter.game.entity.EntityMonster
;
import
emu.grasscutter.game.entity.GameEntity
;
import
emu.grasscutter.scripts.constants.EventType
;
import
emu.grasscutter.scripts.data.SceneGroup
;
import
emu.grasscutter.scripts.data.SceneMonster
;
import
emu.grasscutter.scripts.data.ScriptArgs
;
import
emu.grasscutter.server.packet.send.PacketGadgetStateNotify
;
import
emu.grasscutter.server.packet.send.PacketWorktopOptionNotify
;
...
...
@@ -130,7 +132,7 @@ public class ScriptLib {
getSceneScriptManager
().
getScene
().
addEntities
(
toAdd
);
for
(
GameEntity
entity
:
toAdd
)
{
this
.
getSceneScriptManager
().
onMonsterSpawn
((
EntityMonster
)
entity
);
this
.
getSceneScriptManager
().
callEvent
(
EventType
.
EVENT_ANY_MONSTER_LIVE
,
new
ScriptArgs
(
entity
.
getConfigId
())
);
}
}
...
...
src/main/java/emu/grasscutter/scripts/constants/
Script
EventType.java
→
src/main/java/emu/grasscutter/scripts/constants/EventType.java
View file @
8ab2b446
package
emu.grasscutter.scripts.constants
;
public
class
Script
EventType
{
public
class
EventType
{
public
static
final
int
EVENT_NONE
=
0
;
public
static
final
int
EVENT_ANY_MONSTER_DIE
=
1
;
public
static
final
int
EVENT_ANY_GADGET_DIE
=
2
;
...
...
src/main/java/emu/grasscutter/scripts/data/ScriptArgs.java
0 → 100644
View file @
8ab2b446
package
emu.grasscutter.scripts.data
;
public
class
ScriptArgs
{
public
int
param1
;
public
int
param2
;
public
int
param3
;
public
ScriptArgs
()
{
}
public
ScriptArgs
(
int
param1
)
{
this
.
param1
=
param1
;
}
public
ScriptArgs
(
int
param1
,
int
param2
)
{
this
.
param1
=
param1
;
this
.
param2
=
param2
;
}
public
int
getParam1
()
{
return
param1
;
}
public
ScriptArgs
setParam1
(
int
param1
)
{
this
.
param1
=
param1
;
return
this
;
}
public
int
getParam2
()
{
return
param2
;
}
public
ScriptArgs
setParam2
(
int
param2
)
{
this
.
param2
=
param2
;
return
this
;
}
public
int
getParam3
()
{
return
param3
;
}
public
ScriptArgs
setParam3
(
int
param3
)
{
this
.
param3
=
param3
;
return
this
;
}
}
src/main/java/emu/grasscutter/server/packet/recv/HandlerSelectWorktopOptionReq.java
View file @
8ab2b446
package
emu.grasscutter.server.packet.recv
;
import
emu.grasscutter.game.entity.EntityGadget
;
import
emu.grasscutter.game.entity.GameEntity
;
import
emu.grasscutter.net.packet.Opcodes
;
import
emu.grasscutter.net.packet.PacketOpcodes
;
import
emu.grasscutter.net.proto.SelectWorktopOptionReqOuterClass.SelectWorktopOptionReq
;
import
emu.grasscutter.scripts.constants.EventType
;
import
emu.grasscutter.scripts.data.ScriptArgs
;
import
emu.grasscutter.net.packet.PacketHandler
;
import
emu.grasscutter.server.game.GameSession
;
import
emu.grasscutter.server.packet.send.PacketSelectWorktopOptionRsp
;
...
...
@@ -14,9 +18,21 @@ public class HandlerSelectWorktopOptionReq extends PacketHandler {
public
void
handle
(
GameSession
session
,
byte
[]
header
,
byte
[]
payload
)
throws
Exception
{
SelectWorktopOptionReq
req
=
SelectWorktopOptionReq
.
parseFrom
(
payload
);
session
.
getPlayer
().
getScene
().
getScriptManager
().
onOptionSelect
(
req
.
getGadgetEntityId
(),
req
.
getOptionId
());
try
{
GameEntity
entity
=
session
.
getPlayer
().
getScene
().
getEntityById
(
req
.
getGadgetEntityId
());
if
(
entity
==
null
||
!(
entity
instanceof
EntityGadget
))
{
return
;
}
session
.
getPlayer
().
getScene
().
getScriptManager
().
callEvent
(
EventType
.
EVENT_SELECT_OPTION
,
new
ScriptArgs
(
entity
.
getConfigId
(),
req
.
getOptionId
())
);
}
finally
{
// Always send packet
session
.
send
(
new
PacketSelectWorktopOptionRsp
(
req
.
getGadgetEntityId
(),
req
.
getOptionId
()));
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment