Skip to content

Commit 5f3a93e

Browse files
committed
Replace matches to prefilter builder
1 parent 9470c8d commit 5f3a93e

File tree

5 files changed

+97
-19
lines changed

5 files changed

+97
-19
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,4 @@ persistence.json
343343
src/main/resources/apps.methodscript.com/tsp-output
344344

345345
# VSCode
346-
.vscode
346+
.vscode/*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.laytonsmith.abstraction.events;
2+
3+
import com.laytonsmith.abstraction.MCEntity;
4+
import com.laytonsmith.abstraction.enums.MCEntityType;
5+
import com.laytonsmith.core.events.BindableEvent;
6+
7+
public interface MCEntityEvent extends BindableEvent {
8+
9+
MCEntity getEntity();
10+
11+
MCEntityType getEntityType();
12+
}
Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
package com.laytonsmith.abstraction.events;
22

3-
import com.laytonsmith.abstraction.MCEntity;
4-
import com.laytonsmith.abstraction.enums.MCEntityType;
5-
import com.laytonsmith.core.events.BindableEvent;
63

7-
public interface MCEntityToggleSwimEvent extends BindableEvent {
4+
public interface MCEntityToggleSwimEvent extends MCEntityEvent {
85

96
boolean isSwimming();
10-
11-
MCEntity getEntity();
12-
13-
MCEntityType getEntityType();
147
}

src/main/java/com/laytonsmith/core/events/drivers/EntityEvents.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@
7777
import com.laytonsmith.core.events.EventBuilder;
7878
import com.laytonsmith.core.events.Prefilters;
7979
import com.laytonsmith.core.events.Prefilters.PrefilterType;
80+
import com.laytonsmith.core.events.prefilters.OptionalPlayerPrefilterMatcher;
81+
import com.laytonsmith.core.events.prefilters.PrefilterBuilder;
82+
import com.laytonsmith.core.events.prefilters.StringICPrefilterMatcher;
8083
import com.laytonsmith.core.exceptions.CRE.CREBadEntityException;
8184
import com.laytonsmith.core.exceptions.CRE.CREBindException;
8285
import com.laytonsmith.core.exceptions.CRE.CRECastException;
@@ -2074,16 +2077,23 @@ public String docs() {
20742077
+ " {}";
20752078
}
20762079

2077-
@Override
2078-
public boolean matches(Map<String, Mixed> filter, MCEntityToggleSwimEvent e) throws PrefilterNonMatchException {
2079-
MCEntity entity = e.getEntity();
2080-
Prefilters.match(filter, "type", entity.getType().name(), Prefilters.PrefilterType.MACRO);
2081-
Prefilters.match(filter, "id", entity.getUniqueId().toString(), Prefilters.PrefilterType.MACRO);
20822080

2083-
if(entity instanceof MCPlayer) {
2084-
Prefilters.match(filter, "player", ((MCPlayer) entity).getName(), Prefilters.PrefilterType.MACRO);
2085-
}
2086-
return true;
2081+
@Override
2082+
protected PrefilterBuilder getPrefilterBuilder() {
2083+
return new PrefilterBuilder<MCEntityToggleSwimEvent>()
2084+
.set("player", "The player that toggle swime", new OptionalPlayerPrefilterMatcher<>())
2085+
.set("type", "The entity type of the entity that toggle swime", new StringICPrefilterMatcher<>() {
2086+
@Override
2087+
protected String getProperty(MCEntityToggleSwimEvent event) {
2088+
return event.getEntityType().name();
2089+
}
2090+
})
2091+
.set("id", "The ID of the entity that toggle swime", new StringICPrefilterMatcher<>() {
2092+
@Override
2093+
protected String getProperty(MCEntityToggleSwimEvent event) {
2094+
return event.getEntity().getUniqueId().toString();
2095+
}
2096+
});
20872097
}
20882098

20892099
@Override
@@ -2125,7 +2135,8 @@ public Driver driver() {
21252135
}
21262136

21272137
@Override
2128-
public void cancel(MCEntityToggleSwimEvent o, boolean state) {}
2138+
public void cancel(MCEntityToggleSwimEvent o, boolean state) {
2139+
}
21292140

21302141
@Override
21312142
public boolean isCancellable(MCEntityToggleSwimEvent o) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.laytonsmith.core.events.prefilters;
2+
3+
import com.laytonsmith.abstraction.MCEntity;
4+
import com.laytonsmith.abstraction.MCPlayer;
5+
import com.laytonsmith.abstraction.events.MCEntityEvent;
6+
import com.laytonsmith.core.ParseTree;
7+
import com.laytonsmith.core.compiler.CompilerEnvironment;
8+
import com.laytonsmith.core.compiler.CompilerWarning;
9+
import com.laytonsmith.core.constructs.CClassType;
10+
import com.laytonsmith.core.constructs.CNull;
11+
import com.laytonsmith.core.constructs.CString;
12+
import com.laytonsmith.core.constructs.Target;
13+
import com.laytonsmith.core.environments.Environment;
14+
import com.laytonsmith.core.events.prefilters.PlayerPrefilterMatcher.PlayerPrefilterDocs;
15+
import com.laytonsmith.core.exceptions.ConfigCompileException;
16+
import com.laytonsmith.core.exceptions.ConfigCompileGroupException;
17+
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
18+
import com.laytonsmith.core.natives.interfaces.Mixed;
19+
20+
/**
21+
* A OptionalPlayerPrefilterMatcher is a relatively specialized PrefilterMatcher which only works with MCEntityEvent subtypes.
22+
* It simply checks that entity is a player and matches the player name against the specified input.
23+
*
24+
* @param <T>
25+
*/
26+
public class OptionalPlayerPrefilterMatcher<T extends MCEntityEvent> extends MacroICPrefilterMatcher<T> {
27+
28+
@Override
29+
public PrefilterDocs getDocsObject() {
30+
return new PlayerPrefilterDocs();
31+
}
32+
33+
@Override
34+
public void validate(ParseTree node, CClassType nodeType, Environment env)
35+
throws ConfigCompileException, ConfigCompileGroupException, ConfigRuntimeException {
36+
if(!nodeType.doesExtend(CString.TYPE)) {
37+
env.getEnv(CompilerEnvironment.class).addCompilerWarning(node.getFileOptions(),
38+
new CompilerWarning("Expected a string (player) here, this may not perform as expected.",
39+
node.getTarget(), null));
40+
}
41+
}
42+
43+
@Override
44+
public boolean matches(String key, Mixed value, T event, Target t) {
45+
Object prop = getProperty(event);
46+
if(prop == null) {
47+
return CNull.NULL.equals(value);
48+
}
49+
50+
return super.matches(key, value, event, t);
51+
}
52+
53+
@Override
54+
protected Object getProperty(T event) {
55+
MCEntity entity = event.getEntity();
56+
if(entity instanceof MCPlayer player) {
57+
return player.getName();
58+
}
59+
60+
return null;
61+
}
62+
}

0 commit comments

Comments
 (0)