Skip to content

Commit e20c4fd

Browse files
committed
Merge pull request #379 from PseudoKnight/master
Use method detection instead of version detection when possible
2 parents 208ab76 + 0485453 commit e20c4fd

File tree

5 files changed

+25
-21
lines changed

5 files changed

+25
-21
lines changed

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCObjective.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import com.laytonsmith.abstraction.MCScore;
66
import com.laytonsmith.abstraction.MCScoreboard;
77
import com.laytonsmith.abstraction.enums.MCDisplaySlot;
8-
import com.laytonsmith.abstraction.enums.MCVersion;
98
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCDisplaySlot;
10-
import com.laytonsmith.core.Static;
119
import org.bukkit.Bukkit;
1210
import org.bukkit.OfflinePlayer;
1311
import org.bukkit.scoreboard.DisplaySlot;
@@ -47,9 +45,10 @@ public String getName() {
4745

4846
@Override
4947
public MCScore getScore(String entry) {
50-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_7_10)){
48+
if(ReflectionUtils.hasMethod(o.getClass(), "getScore", null, String.class)){
5149
return new BukkitMCScore(o.getScore(entry));
5250
} else {
51+
// Probably 1.7.8 or prior
5352
OfflinePlayer player = Bukkit.getOfflinePlayer(entry);
5453
return new BukkitMCScore((Score) ReflectionUtils.invokeMethod(o, "getScore", player));
5554
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCScoreboard.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import com.laytonsmith.abstraction.MCScoreboard;
88
import com.laytonsmith.abstraction.MCTeam;
99
import com.laytonsmith.abstraction.enums.MCDisplaySlot;
10-
import com.laytonsmith.abstraction.enums.MCVersion;
1110
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCDisplaySlot;
12-
import com.laytonsmith.core.Static;
1311
import org.bukkit.Bukkit;
1412
import org.bukkit.OfflinePlayer;
1513
import org.bukkit.scoreboard.Objective;
@@ -74,10 +72,10 @@ public Set<MCObjective> getObjectivesByCriteria(String criteria) {
7472

7573
@Override
7674
public Set<String> getEntries() {
77-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_7_10)){
75+
if(ReflectionUtils.hasMethod(s.getClass(), "getEntries", null)){
7876
return s.getEntries();
7977
} else {
80-
// Old style, where we have to build it from the list of players
78+
// Probably 1.7.8 or prior
8179
Set<String> ret = new HashSet<>();
8280
for (OfflinePlayer o : (Set<OfflinePlayer>) ReflectionUtils.invokeMethod(s, "getPlayers")) {
8381
ret.add(o.getName());
@@ -98,11 +96,12 @@ public MCTeam getPlayerTeam(MCOfflinePlayer player) {
9896
@Override
9997
public Set<MCScore> getScores(String entry) {
10098
Set<MCScore> ret = new HashSet<>();
101-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_7_10)){
99+
if(ReflectionUtils.hasMethod(s.getClass(), "getScores", null, String.class)){
102100
for (Score o : s.getScores(entry)) {
103101
ret.add(new BukkitMCScore(o));
104102
}
105103
} else {
104+
// Probably 1.7.8 or prior
106105
OfflinePlayer player = Bukkit.getOfflinePlayer(entry);
107106
for (Score o : (Set<Score>) ReflectionUtils.invokeMethod(s, "getScores", player)) {
108107
ret.add(new BukkitMCScore(o));
@@ -141,9 +140,10 @@ public MCTeam registerNewTeam(String name) {
141140

142141
@Override
143142
public void resetScores(String entry) {
144-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_7_10)){
143+
if(ReflectionUtils.hasMethod(s.getClass(), "resetScores", null, String.class)){
145144
s.resetScores(entry);
146145
} else {
146+
// Probably 1.7.8 or prior
147147
OfflinePlayer player = Bukkit.getOfflinePlayer(entry);
148148
ReflectionUtils.invokeMethod(s, "resetScores", player);
149149
}

src/main/java/com/laytonsmith/abstraction/bukkit/BukkitMCTeam.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
import java.util.Set;
88

99
import com.laytonsmith.abstraction.enums.MCNameTagVisibility;
10-
import com.laytonsmith.abstraction.enums.MCVersion;
1110
import com.laytonsmith.abstraction.enums.bukkit.BukkitMCNameTagVisibility;
12-
import com.laytonsmith.core.Static;
1311
import org.bukkit.Bukkit;
1412
import org.bukkit.OfflinePlayer;
1513
import org.bukkit.scoreboard.NameTagVisibility;
@@ -24,9 +22,10 @@ public BukkitMCTeam(Team team) {
2422

2523
@Override
2624
public void addEntry(String entry) {
27-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8_7)){
25+
if(ReflectionUtils.hasMethod(t.getClass(), "addEntry", null, String.class)){
2826
t.addEntry(entry);
2927
} else {
28+
// Probably 1.8.5 or prior
3029
OfflinePlayer player = Bukkit.getOfflinePlayer(entry);
3130
ReflectionUtils.invokeMethod(t, "addPlayer", player);
3231
}
@@ -61,11 +60,12 @@ public MCNameTagVisibility getNameTagVisibility() {
6160
@Override
6261
public Set<String> getEntries() {
6362
Set<String> ret = new HashSet<String>();
64-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8_7)){
63+
if(ReflectionUtils.hasMethod(t.getClass(), "getEntries", null)){
6564
for (String e : t.getEntries()) {
6665
ret.add(e);
6766
}
6867
} else {
68+
// Probably 1.8.5 or prior
6969
for (OfflinePlayer o : (Set<OfflinePlayer>) ReflectionUtils.invokeMethod(t, "getPlayers")) {
7070
ret.add(o.getName());
7171
}
@@ -95,19 +95,21 @@ public String getSuffix() {
9595

9696
@Override
9797
public boolean hasEntry(String entry) {
98-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8_7)){
98+
if(ReflectionUtils.hasMethod(t.getClass(), "hasEntry", null, String.class)){
9999
return t.hasEntry(entry);
100100
} else {
101+
// Probably 1.8.5 or prior
101102
OfflinePlayer player = Bukkit.getOfflinePlayer(entry);
102103
return (boolean) ReflectionUtils.invokeMethod(t, "hasPlayer", player);
103104
}
104105
}
105106

106107
@Override
107108
public boolean removeEntry(String entry) {
108-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8_7)){
109+
if(ReflectionUtils.hasMethod(t.getClass(), "removeEntry", null, String.class)){
109110
return t.removeEntry(entry);
110111
} else {
112+
// Probably 1.8.5 or prior
111113
OfflinePlayer player = Bukkit.getOfflinePlayer(entry);
112114
return (boolean) ReflectionUtils.invokeMethod(t, "removePlayer", player);
113115
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCArmorStand.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.laytonsmith.abstraction.bukkit.entities;
22

3+
import com.laytonsmith.PureUtilities.Common.ReflectionUtils;
34
import com.laytonsmith.PureUtilities.Vector3D;
45
import com.laytonsmith.abstraction.MCArmorStand;
56
import com.laytonsmith.abstraction.MCEntityEquipment;
67
import com.laytonsmith.abstraction.MCItemStack;
78
import com.laytonsmith.abstraction.bukkit.BukkitMCEntityEquipment;
89
import com.laytonsmith.abstraction.bukkit.BukkitMCItemStack;
910
import com.laytonsmith.abstraction.enums.MCBodyPart;
10-
import com.laytonsmith.abstraction.enums.MCVersion;
11-
import com.laytonsmith.core.Static;
1211
import org.bukkit.entity.ArmorStand;
1312
import org.bukkit.entity.Entity;
1413
import org.bukkit.entity.LivingEntity;
@@ -265,15 +264,17 @@ public void setSmall(boolean small) {
265264

266265
@Override
267266
public Boolean isMarker() {
268-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8_7)) {
267+
// Added in 1.8.7
268+
if(ReflectionUtils.hasMethod(as.getClass(), "isMarker", null)){
269269
return as.isMarker();
270270
}
271271
return null;
272272
}
273273

274274
@Override
275275
public void setMarker(boolean marker) {
276-
if(Static.getServer().getMinecraftVersion().gte(MCVersion.MC1_8_7)) {
276+
// Added in 1.8.7
277+
if(ReflectionUtils.hasMethod(as.getClass(), "setMarker", null, boolean.class)){
277278
as.setMarker(marker);
278279
}
279280
}

src/main/java/com/laytonsmith/abstraction/bukkit/entities/BukkitMCPlayer.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,8 @@ public void setRemainingFireTicks(int i) {
401401

402402
@Override
403403
public void setSpectatorTarget(MCEntity entity) {
404-
if(Static.getServer().getMinecraftVersion().lt(MCVersion.MC1_8_7)) {
404+
if(!ReflectionUtils.hasMethod(p.getClass(), "setSpectatorTarget", null, Entity.class)){
405+
// Probably 1.8.6 or prior
405406
return;
406407
}
407408
if(entity == null) {
@@ -413,7 +414,8 @@ public void setSpectatorTarget(MCEntity entity) {
413414

414415
@Override
415416
public MCEntity getSpectatorTarget() {
416-
if(Static.getServer().getMinecraftVersion().lt(MCVersion.MC1_8_7)) {
417+
if(!ReflectionUtils.hasMethod(p.getClass(), "getSpectatorTarget", null)){
418+
// Probably 1.8.6 or prior
417419
return null;
418420
}
419421
return BukkitConvertor.BukkitGetCorrectEntity(p.getSpectatorTarget());

0 commit comments

Comments
 (0)