Skip to content

Commit 316e574

Browse files
Fixes and update Minecarts (#8064)
* Fix and update Minecarts * Update src/main/java/ch/njol/skript/entity/MinecartData.java * Update src/main/java/ch/njol/skript/entity/MinecartData.java Co-authored-by: SirSmurfy2 <[email protected]> * Update src/main/java/ch/njol/skript/entity/MinecartData.java Co-authored-by: SirSmurfy2 <[email protected]> * Update src/main/java/ch/njol/skript/entity/MinecartData.java Co-authored-by: Patrick Miller <[email protected]>
1 parent 8dd18bc commit 316e574

File tree

2 files changed

+66
-95
lines changed

2 files changed

+66
-95
lines changed

src/main/java/ch/njol/skript/entity/EntityData.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ public final String toString() {
399399
return toString(0);
400400
}
401401

402-
@SuppressWarnings("null")
403402
protected Noun getName() {
404403
return info.names[matchedPattern];
405404
}
@@ -408,7 +407,6 @@ protected Noun getName() {
408407
return baby.isTrue() ? m_baby : baby.isFalse() ? m_adult : null;
409408
}
410409

411-
@SuppressWarnings("null")
412410
public String toString(int flags) {
413411
Noun name = info.names[matchedPattern];
414412
return baby.isTrue() ? m_baby.toString(name, flags) : baby.isFalse() ? m_adult.toString(name, flags) : name.toString(flags);
@@ -511,7 +509,6 @@ public static EntityDataInfo<?> getInfo(Class<? extends EntityData<?>> entityDat
511509
* @param string String with optional indefinite article at the beginning
512510
* @return The parsed entity data
513511
*/
514-
@SuppressWarnings("null")
515512
public static @Nullable EntityData<?> parse(String string) {
516513
Iterator<EntityDataInfo<EntityData<?>>> it = new ArrayList<>(infos).iterator();
517514
return SkriptParser.parseStatic(Noun.stripIndefiniteArticle(string), it, null);
@@ -806,15 +803,14 @@ protected boolean deserialize(String string) {
806803
World world = location.getWorld();
807804
if (world == null)
808805
return null;
809-
try {
810-
if (WORLD_1_17_CONSUMER) {
811-
return (@Nullable E) WORLD_1_17_CONSUMER_METHOD.invoke(world, location, type,
812-
(org.bukkit.util.Consumer<E>) consumer::accept);
806+
if (WORLD_1_17_CONSUMER) {
807+
try {
808+
return (@Nullable E) WORLD_1_17_CONSUMER_METHOD.invoke(world, location, type, (org.bukkit.util.Consumer<E>) consumer::accept);
809+
} catch (InvocationTargetException | IllegalAccessException e) {
810+
if (Skript.testing())
811+
Skript.exception(e, "Can't spawn " + type.getName());
812+
return null;
813813
}
814-
} catch (InvocationTargetException | IllegalAccessException e) {
815-
if (Skript.testing())
816-
Skript.exception(e, "Can't spawn " + type.getName());
817-
return null;
818814
}
819815
return world.spawn(location, type, consumer);
820816
}
Lines changed: 59 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package ch.njol.skript.entity;
22

33
import java.util.ArrayList;
4-
54
import org.bukkit.entity.Minecart;
65
import org.bukkit.entity.minecart.CommandMinecart;
76
import org.bukkit.entity.minecart.ExplosiveMinecart;
@@ -12,142 +11,118 @@
1211
import org.bukkit.entity.minecart.StorageMinecart;
1312
import org.jetbrains.annotations.NotNull;
1413
import org.jetbrains.annotations.Nullable;
15-
1614
import ch.njol.skript.lang.Literal;
1715
import ch.njol.skript.lang.SkriptParser.ParseResult;
18-
import ch.njol.skript.util.Utils;
1916
import ch.njol.skript.variables.Variables;
2017

21-
/**
22-
* @author Peter Güttinger
23-
*/
2418
public class MinecartData extends EntityData<Minecart> {
25-
26-
@SuppressWarnings("null")
19+
2720
private static enum MinecartType {
28-
ANY(Minecart.class, "minecart"),
2921
NORMAL(RideableMinecart.class, "regular minecart"),
3022
STORAGE(StorageMinecart.class, "storage minecart"),
3123
POWERED(PoweredMinecart.class, "powered minecart"),
3224
HOPPER(HopperMinecart.class, "hopper minecart"),
3325
EXPLOSIVE(ExplosiveMinecart.class, "explosive minecart"),
3426
SPAWNER(SpawnerMinecart.class, "spawner minecart"),
3527
COMMAND(CommandMinecart.class, "command minecart");
36-
37-
@Nullable
38-
final Class<? extends Minecart> c;
28+
29+
private final Class<? extends Minecart> entityClass;
3930
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;
4334
this.codeName = codeName;
4435
}
45-
36+
4637
@Override
4738
public String toString() {
4839
return codeName;
4940
}
50-
41+
5142
public static String[] codeNames;
5243
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);
5748
}
58-
codeNames = cn.toArray(new String[0]);
49+
codeNames = names.toArray(new String[0]);
5950
}
6051
}
61-
52+
6253
static {
6354
EntityData.register(MinecartData.class, "minecart", Minecart.class, 0, MinecartType.codeNames);
64-
6555
Variables.yggdrasil.registerSingleClass(MinecartType.class, "MinecartType");
6656
}
67-
68-
private MinecartType type = MinecartType.ANY;
69-
57+
58+
private MinecartType type = MinecartType.NORMAL;
59+
private boolean isSupertype = true;
60+
7061
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;
7366
this.type = type;
74-
this.matchedPattern = type.ordinal();
7567
}
76-
77-
@SuppressWarnings("null")
68+
7869
@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;
8177
return true;
8278
}
83-
84-
@SuppressWarnings("null")
79+
8580
@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;
9588
}
9689
}
97-
assert false;
98-
return false;
90+
return true;
9991
}
100-
92+
10193
@Override
102-
public void set(final Minecart entity) {}
103-
94+
public void set(Minecart entity) {}
95+
10496
@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);
11099
}
111-
100+
112101
@Override
113102
public Class<? extends Minecart> getType() {
114-
return type.c != null ? type.c : Minecart.class;
103+
return isSupertype ? Minecart.class : type.entityClass;
115104
}
116-
105+
117106
@Override
118107
protected int hashCode_i() {
119108
return type.hashCode();
120109
}
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+
131111
@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;
139116
}
140-
117+
141118
@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);
146121
}
147-
122+
148123
@Override
149-
public @NotNull EntityData getSuperType() {
150-
return new MinecartData(type);
124+
public @NotNull EntityData<?> getSuperType() {
125+
return new MinecartData();
151126
}
152-
127+
153128
}

0 commit comments

Comments
 (0)