|
1 | 1 | package ch.njol.skript.entity; |
2 | 2 |
|
3 | 3 | import java.util.ArrayList; |
4 | | - |
5 | 4 | import org.bukkit.entity.Minecart; |
6 | 5 | import org.bukkit.entity.minecart.CommandMinecart; |
7 | 6 | import org.bukkit.entity.minecart.ExplosiveMinecart; |
|
12 | 11 | import org.bukkit.entity.minecart.StorageMinecart; |
13 | 12 | import org.jetbrains.annotations.NotNull; |
14 | 13 | import org.jetbrains.annotations.Nullable; |
15 | | - |
16 | 14 | import ch.njol.skript.lang.Literal; |
17 | 15 | import ch.njol.skript.lang.SkriptParser.ParseResult; |
18 | | -import ch.njol.skript.util.Utils; |
19 | 16 | import ch.njol.skript.variables.Variables; |
20 | 17 |
|
21 | | -/** |
22 | | - * @author Peter Güttinger |
23 | | - */ |
24 | 18 | public class MinecartData extends EntityData<Minecart> { |
25 | | - |
26 | | - @SuppressWarnings("null") |
| 19 | + |
27 | 20 | private static enum MinecartType { |
28 | | - ANY(Minecart.class, "minecart"), |
29 | 21 | NORMAL(RideableMinecart.class, "regular minecart"), |
30 | 22 | STORAGE(StorageMinecart.class, "storage minecart"), |
31 | 23 | POWERED(PoweredMinecart.class, "powered minecart"), |
32 | 24 | HOPPER(HopperMinecart.class, "hopper minecart"), |
33 | 25 | EXPLOSIVE(ExplosiveMinecart.class, "explosive minecart"), |
34 | 26 | SPAWNER(SpawnerMinecart.class, "spawner minecart"), |
35 | 27 | COMMAND(CommandMinecart.class, "command minecart"); |
36 | | - |
37 | | - @Nullable |
38 | | - final Class<? extends Minecart> c; |
| 28 | + |
| 29 | + private final Class<? extends Minecart> entityClass; |
39 | 30 | private final String codeName; |
40 | | - |
41 | | - MinecartType(final @Nullable Class<? extends Minecart> c, final String codeName) { |
42 | | - this.c = c; |
| 31 | + |
| 32 | + MinecartType(Class<? extends Minecart> entityClass, String codeName) { |
| 33 | + this.entityClass = entityClass; |
43 | 34 | this.codeName = codeName; |
44 | 35 | } |
45 | | - |
| 36 | + |
46 | 37 | @Override |
47 | 38 | public String toString() { |
48 | 39 | return codeName; |
49 | 40 | } |
50 | | - |
| 41 | + |
51 | 42 | public static String[] codeNames; |
52 | 43 | static { |
53 | | - final ArrayList<String> cn = new ArrayList<>(); |
54 | | - for (final MinecartType t : values()) { |
55 | | - if (t.c != null) |
56 | | - cn.add(t.codeName); |
| 44 | + var names = new ArrayList<>(); |
| 45 | + names.add("minecart"); |
| 46 | + for (MinecartType type : values()) { |
| 47 | + names.add(type.codeName); |
57 | 48 | } |
58 | | - codeNames = cn.toArray(new String[0]); |
| 49 | + codeNames = names.toArray(new String[0]); |
59 | 50 | } |
60 | 51 | } |
61 | | - |
| 52 | + |
62 | 53 | static { |
63 | 54 | EntityData.register(MinecartData.class, "minecart", Minecart.class, 0, MinecartType.codeNames); |
64 | | - |
65 | 55 | Variables.yggdrasil.registerSingleClass(MinecartType.class, "MinecartType"); |
66 | 56 | } |
67 | | - |
68 | | - private MinecartType type = MinecartType.ANY; |
69 | | - |
| 57 | + |
| 58 | + private MinecartType type = MinecartType.NORMAL; |
| 59 | + private boolean isSupertype = true; |
| 60 | + |
70 | 61 | public MinecartData() {} |
71 | | - |
72 | | - public MinecartData(final MinecartType type) { |
| 62 | + |
| 63 | + public MinecartData(MinecartType type) { |
| 64 | + this.matchedPattern = type.ordinal() + 1; |
| 65 | + this.isSupertype = false; |
73 | 66 | this.type = type; |
74 | | - this.matchedPattern = type.ordinal(); |
75 | 67 | } |
76 | | - |
77 | | - @SuppressWarnings("null") |
| 68 | + |
78 | 69 | @Override |
79 | | - protected boolean init(final Literal<?>[] exprs, final int matchedPattern, final ParseResult parseResult) { |
80 | | - type = MinecartType.values()[matchedPattern]; |
| 70 | + protected boolean init(Literal<?>[] exprs, int matchedPattern, ParseResult parseResult) { |
| 71 | + if (matchedPattern == 0) |
| 72 | + return true; |
| 73 | + |
| 74 | + // Avoid the first codeName, as the first codeName is used for super type any comparison |
| 75 | + type = MinecartType.values()[--matchedPattern]; |
| 76 | + isSupertype = false; |
81 | 77 | return true; |
82 | 78 | } |
83 | | - |
84 | | - @SuppressWarnings("null") |
| 79 | + |
85 | 80 | @Override |
86 | | - protected boolean init(final @Nullable Class<? extends Minecart> c, final @Nullable Minecart e) { |
87 | | - final MinecartType[] ts = MinecartType.values(); |
88 | | - for (int i = ts.length - 1; i >= 0; i--) { |
89 | | - final Class<?> mc = ts[i].c; |
90 | | - if (mc == null) |
91 | | - continue; |
92 | | - if (e == null ? mc.isAssignableFrom(c) : mc.isInstance(e)) { |
93 | | - type = ts[i]; |
94 | | - return true; |
| 81 | + protected boolean init(@Nullable Class<? extends Minecart> entityClass, @Nullable Minecart entity) { |
| 82 | + for (MinecartType t : MinecartType.values()) { |
| 83 | + Class<?> mc = t.entityClass; |
| 84 | + if (entity == null ? mc.isAssignableFrom(entityClass) : mc.isInstance(entity)) { |
| 85 | + isSupertype = false; |
| 86 | + type = t; |
| 87 | + break; |
95 | 88 | } |
96 | 89 | } |
97 | | - assert false; |
98 | | - return false; |
| 90 | + return true; |
99 | 91 | } |
100 | | - |
| 92 | + |
101 | 93 | @Override |
102 | | - public void set(final Minecart entity) {} |
103 | | - |
| 94 | + public void set(Minecart entity) {} |
| 95 | + |
104 | 96 | @Override |
105 | | - public boolean match(final Minecart entity) { |
106 | | - if (type == MinecartType.NORMAL && type.c == Minecart.class) // pre-1.5 |
107 | | - return !(entity.getClass().equals(Utils.classForName("org.bukkit.entity.StorageMinecart")) |
108 | | - || entity.getClass().equals(Utils.classForName("org.bukkit.entity.PoweredMinecart"))); |
109 | | - return type.c != null && type.c.isInstance(entity); |
| 97 | + public boolean match(Minecart entity) { |
| 98 | + return isSupertype || type.entityClass.isInstance(entity); |
110 | 99 | } |
111 | | - |
| 100 | + |
112 | 101 | @Override |
113 | 102 | public Class<? extends Minecart> getType() { |
114 | | - return type.c != null ? type.c : Minecart.class; |
| 103 | + return isSupertype ? Minecart.class : type.entityClass; |
115 | 104 | } |
116 | | - |
| 105 | + |
117 | 106 | @Override |
118 | 107 | protected int hashCode_i() { |
119 | 108 | return type.hashCode(); |
120 | 109 | } |
121 | | - |
122 | | - @Override |
123 | | - protected boolean equals_i(final EntityData<?> obj) { |
124 | | - if (!(obj instanceof MinecartData)) |
125 | | - return false; |
126 | | - final MinecartData other = (MinecartData) obj; |
127 | | - return type == other.type; |
128 | | - } |
129 | | - |
130 | | -// return type.name(); |
| 110 | + |
131 | 111 | @Override |
132 | | - protected boolean deserialize(final String s) { |
133 | | - try { |
134 | | - type = MinecartType.valueOf(s); |
135 | | - return true; |
136 | | - } catch (final IllegalArgumentException e) { |
137 | | - return false; |
138 | | - } |
| 112 | + protected boolean equals_i(EntityData<?> obj) { |
| 113 | + return obj instanceof MinecartData minecartData && |
| 114 | + isSupertype == minecartData.isSupertype && |
| 115 | + type == minecartData.type; |
139 | 116 | } |
140 | | - |
| 117 | + |
141 | 118 | @Override |
142 | | - public boolean isSupertypeOf(final EntityData<?> e) { |
143 | | - if (e instanceof MinecartData) |
144 | | - return type == MinecartType.ANY || ((MinecartData) e).type == type; |
145 | | - return false; |
| 119 | + public boolean isSupertypeOf(EntityData<?> other) { |
| 120 | + return other instanceof MinecartData minecartData && (isSupertype || type == minecartData.type); |
146 | 121 | } |
147 | | - |
| 122 | + |
148 | 123 | @Override |
149 | | - public @NotNull EntityData getSuperType() { |
150 | | - return new MinecartData(type); |
| 124 | + public @NotNull EntityData<?> getSuperType() { |
| 125 | + return new MinecartData(); |
151 | 126 | } |
152 | | - |
| 127 | + |
153 | 128 | } |
0 commit comments